Angular:如何强制重新编译指令? [英] Angular: How to force recompile directive?

查看:613
本文介绍了Angular:如何强制重新编译指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML:

<div ng-repeat="obj in arr">
   <custom-directive status-stored-in="obj"></custom-directive>
</div>

问题:

我有页面 - 内置了大量的 obj s。这意味着 arr 的值将会改变,代表 obj 的当前页面。但是, status-stored-in =obj部分中的 obj 不会随着更改而刷新。

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 obj in the status-stored-in="obj" part is not refreshed with the change.

现在我的解决方案是在 customDirective ng-if $ c>,来回闪烁它的值以强制重新编译。还有其他等效的,更简洁的方法来解决这个问题吗?

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?

自定义的开始指令:

module.directive 'checkbox', (checkboxHooks) ->
  restrict: 'E'
  scope:
    hook: '='
    hookedTo: '='
    statusStoredIn: '='
  templateUrl: 'templates/checkbox.html'
  link: (scope, element, attr) ->

要点是它需要获取一个对象,用于存储选中状态。整个过程可以在这里找到:[咖啡 / js ] 。

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].

推荐答案

在指令链接功能中,您需要观察 status-stored-in for changes然后重新编译它,例如:

Inside your directives link function you need to watch status-stored-in for changes and then recompile it e.g.:

   link: function(scope, element) {
    scope.$watch('statusStoredIn', function() {
      element.html(statusStoredIn);
      $compile(element.contents())(scope);
    });
   }  

这篇关于Angular:如何强制重新编译指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆