我在html页面中有一堆指令,假设指令是<my-directive></my-directive>
。这些指令在AngularJS 之外添加或更改,例如一个jQuery函数只需append
一个新的<my-directive></my-directive>
页面。
这是我的问题 - 在添加新的<my-directive></my-directive>
之后,该指令实际上没有做任何事情 - 它只是一个没有任何功能的标签。
如何强制Angular识别添加了新标签?我尝试过使用scope.apply,但没有运气。
谢谢!
答案 0 :(得分:1)
我认为你正在寻找角度$compile
方法:
myApp.directive('addonclick', function($compile){
return {
restrict: 'A',
link: function(scope, element, attrs){
var html = attrs.addonclick; //or something else
element.on("click", function(){
$(element).append($compile(html)(scope));
})
};
};
});
编辑:
首先尝试获取范围:
var scope = angular.element("yourElement").scope();
然后获取并调用编译服务:
var compile = angular.element("yourElement").injector().get("$compile"); //or
var compile = angular.injector(["moduleName"]).get("$compile");
$("yourElement").append(compile("<my-directive/>")(scope));