且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带有默认选项的 AngularJS 指令

更新时间:2022-06-23 02:28:39

您可以使用 compile 功能 - 如果未设置属性,则读取它们 - 使用默认值填充它们.

You can use compile function - read attributes if they are not set - fill them with default values.

.directive('pagination', ['$parse', 'paginationConfig', function($parse, config) {
    ...
    controller: 'PaginationController',
    compile: function(element, attrs){
       if (!attrs.attrOne) { attrs.attrOne = 'default value'; }
       if (!attrs.attrTwo) { attrs.attrTwo = 42; }
    },
        ...
  }
});