Angular:如何强制重新编译指令?

时间:2015-02-18 09:19:09

标签: javascript angularjs angularjs-directive

HTML:

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

问题:

我为大量的obj内置了翻页。这意味着arr的值(代表obj的当前页面)将发生变化。但是,obj部分中的status-stored-in="obj"不会因更改而刷新。

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

编辑:

自定义指令的开头:

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

要点是它需要抓住一个对象,以存储checked状态。整个过程可以在这里找到:[coffee / js]。

1 个答案:

答案 0 :(得分:7)

在指令链接功能中,您需要查看status-stored-in进行更改,然后重新编译它,例如:

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