javascript Angular:如何强制重新编译指令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28579849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular: How to force recompile directive?
提问by Lucia
HTML:
HTML:
<div ng-repeat="obj in arr">
<custom-directive status-stored-in="obj"></custom-directive>
</div>
Problem:
问题:
I have page-turn built in for the large amount of obj
s. Which means that the value of arr
, representing the current page of obj
s, will change. However, the obj
in the status-stored-in="obj"
part is not refreshed with the change.
我为大量的obj
s内置了翻页功能。这意味着arr
代表obj
s的当前页面的的值将发生变化。然而,obj
在status-stored-in="obj"
部分不与改变刷新。
Right now my solution is to add a ng-if
in customDirective
, flickering its value back and forth to have it force recompiled. Is there any other equivalent, neater way to deal with this?
现在我的解决方案是添加一个ng-if
in customDirective
,来回闪烁其值以强制重新编译。有没有其他等效的,更简洁的方法来处理这个问题?
Edit:
编辑:
The start of the custom directive:
自定义指令的开始:
module.directive 'checkbox', (checkboxHooks) ->
restrict: 'E'
scope:
hook: '='
hookedTo: '='
statusStoredIn: '='
templateUrl: 'templates/checkbox.html'
link: (scope, element, attr) ->
The gist is that it needs to get hold of an object, for storing the checked
status. The whole of it can be found here: [coffee/js].
回答by Dani?l Smink
Inside your directives link function you need to watch status-stored-in
for changes and then recompile it e.g.:
在您的指令链接函数中,您需要注意status-stored-in
更改,然后重新编译它,例如:
link: function(scope, element) {
scope.$watch('statusStoredIn', function() {
element.html(statusStoredIn);
$compile(element.contents())(scope);
});
}