AngularJS Dynamic Directives inside ng-repeat

只愿长相守 提交于 2019-12-08 04:09:23

One of the issues is order that attributes get resolved into html. They are available in scope earlier in the cycle Here's one way you can do it:

HTML:

<div directive-writer directive-text="dir.text" directive-type="dir.directive"></div>

Directive:

angular.module('app').directive('directiveWriter', function($compile) {
    return {
        restrict: 'A',
        scope:{
          directiveType:'=',
          directiveText:'='
        },
        link:function(scope,elem, attrs){
          var template='<div say="' + scope.directiveText + '" ' + scope.directiveType + '>' + scope.directiveType + '</div>';
          template= $compile(template)(scope);
          elem.replaceWith(template);
        }
    };
});

DEMO

I modified your example. Now it works. You should use $compile.

See this

  (http://plnkr.co/edit/2pUzPClubLLBlfap8fhk?p=preview)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!