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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:12:37  来源:igfitidea点击:

Angular: How to force recompile directive?

javascriptangularjsangularjs-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 objs. Which means that the value of arr, representing the current page of objs, will change. However, the objin the status-stored-in="obj"part is not refreshed with the change.

我为大量的objs内置了翻页功能。这意味着arr代表objs的当前页面的的值将发生变化。然而,objstatus-stored-in="obj"部分不与改变刷新。

Right now my solution is to add a ng-ifin customDirective, flickering its value back and forth to have it force recompiled. Is there any other equivalent, neater way to deal with this?

现在我的解决方案是添加一个ng-ifin 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 checkedstatus. The whole of it can be found here: [coffee/js].

要点是它需要获取一个对象来存储checked状态。全部内容可以在这里找到:[ coffee/ js]。

回答by Dani?l Smink

Inside your directives link function you need to watch status-stored-infor 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);
    });
   }