AngularJS directive with default options

前端 未结 3 666
说谎
说谎 2020-12-12 12:38

I\'m just starting with angularjs, and am working on converting a few old JQuery plugins to Angular directives. I\'d like to define a set of default options for my (element)

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 12:43

    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; }
        },
            ...
      }
    });
    

提交回复
热议问题