[英]angularjs - Reload all directives on page after dynamically updating them
I have a bunch of directives in an html page, let's say the directive is <my-directive></my-directive>
. 我在html页面中有一堆指令,比如说指令是<my-directive></my-directive>
。 These directives are added or changed outside of AngularJS , eg a jQuery function will simply append
a new <my-directive></my-directive>
to the page. 这些指令是在AngularJS之外添加或更改的 ,例如jQuery函数将简单地向页面append
新的<my-directive></my-directive>
。
Here's my question - after appending the new <my-directive></my-directive>
, the directive doesn't actually do anything - it's just a tag without any functionality. 这是我的问题-在添加新的<my-directive></my-directive>
,该指令实际上并没有执行任何操作-它只是一个没有任何功能的标记。
How can I force Angular to recognize that a new tag has been added? 如何强制Angular识别已添加新标签? I've tried mucking around with scope.apply but haven't had any luck. 我已经尝试过合并scope.apply,但是没有任何运气。
Thanks! 谢谢!
I think you're looking for angulars $compile
method: 我认为您正在寻找angulars $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));
})
};
};
});
EDIT: 编辑:
Try getting the scope first: 尝试先获取范围:
var scope = angular.element("yourElement").scope();
Then get and call the compile service: 然后获取并调用编译服务:
var compile = angular.element("yourElement").injector().get("$compile"); //or
var compile = angular.injector(["moduleName"]).get("$compile");
$("yourElement").append(compile("<my-directive/>")(scope));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.