Angularjs dynamic directive inside ngrepeat

前端 未结 2 1468
一个人的身影
一个人的身影 2021-01-05 13:48

Look at example:

$scope.fields = [{
    name: \'Email\',
    dir : \"abc\"
}, {
    name: \'List\',
   dir : \"ffffd\"
}];

app.directive(\'abc\', function ()          


        
2条回答
  •  情深已故
    2021-01-05 14:18

    Try this directive:

    app.directive('dynamicDirective',function($compile){
      return {
          restrict: 'A',
          replace: false, 
          terminal: true, 
          priority: 1000, 
          link:function(scope,element,attrs){
    
            element.attr(scope.$eval(attrs.dynamicDirective),"");//add dynamic directive
    
            element.removeAttr("dynamic-directive"); //remove the attribute to avoid indefinite loop
            element.removeAttr("data-dynamic-directive");
    
            $compile(element)(scope);
          }
      };
    });
    

    Use it:

    DEMO

    For more information on how this directive works and why we have to add terminal:true and priority: 1000. Check out Add directives from directive in AngularJS

提交回复
热议问题