dynamically adding angularjs directives

前端 未结 1 980
栀梦
栀梦 2021-02-11 05:55

I\'m trying to write a directive that dynamically adds directives of another type to the DOM and compile them. It seems to work when using a string template, but fails when usi

1条回答
  •  爱一瞬间的悲伤
    2021-02-11 06:17

    Adding scope.$apply() worked for me. Tested with jQuery 1.9.0 and Angular 1.0.3.

    iElement.bind('click', function() {
        $('body').append($compile(t)(scope));
        scope.$apply();  // cause a $digest cycle to run, since we're "outside" Angular
    });
    

    This fiddle uses an inlined template, but I also tested with a local webserver that had to do a separate HTTP GET to get the partial.

    0 讨论(0)
提交回复
热议问题