Use an angular directive inside another directive

后端 未结 1 1118
灰色年华
灰色年华 2020-12-05 00:48

I have created the below angular directives, ChildDirective that is used inside ParentDirective

var wizardModule = angular.         


        
相关标签:
1条回答
  • 2020-12-05 01:23

    You have to link the compiled element to the scope. And since you're no longer modifying the template element you should append the new elements to the linked element. YOu can do it like this:

    var elem;
    wizardModule.directive('parentDirective', function ($http, $compile) {
    return {
        restrict: 'E',
        compile: function (element, attrs) {
            var controllerurl = attrs.controllerurl;
            elem = element;
    
            if (controllerurl) {
              return function(scope,element){
                $http.get(controllerurl + '/GetWizardItems').
                success(function (data, status, headers, config) {
                    var x = angular.element('<child-directive></child-directive><child-directive></child-directive>');
                    element.append(x);
                    $compile(x)(scope);
                });
              }
            }
        }
    }
    });
    
    0 讨论(0)
提交回复
热议问题