Add directives from directive in AngularJS

后端 未结 7 1730
庸人自扰
庸人自扰 2020-11-22 10:02

I\'m trying to build a directive that takes care of adding more directives to the element it is declared on. For example, I want to build a directive that t

7条回答
  •  庸人自扰
    2020-11-22 10:21

    Try storing the state in a attribute on the element itself, such as superDirectiveStatus="true"

    For example:

    angular.module('app')
      .directive('superDirective', function ($compile, $injector) {
        return {
          restrict: 'A',
          replace: true,
          link: function compile(scope, element, attrs) {
            if (element.attr('datepicker')) { // check
              return;
            }
            var status = element.attr('superDirectiveStatus');
            if( status !== "true" ){
                 element.attr('datepicker', 'someValue');
                 element.attr('datepicker-language', 'en');
                 // some more
                 element.attr('superDirectiveStatus','true');
                 $compile(element)(scope);
    
            }
    
          }
        };
      });
    

    I hope this helps you.

提交回复
热议问题