簡體   English   中英

Angular:如何強制重新編譯指令?

[英]Angular: How to force recompile directive?

HTML:

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

問題:

我為大量的obj內置了翻頁。 這意味着代表obj的當前頁面的arr的值將改變。 然而, objstatus-stored-in="obj"部分不與改變刷新。

現在我的解決方案是在customDirective添加一個ng-ifcustomDirective閃爍它的值以強制重新編譯。 有沒有其他等效,更簡潔的方式來處理這個?

編輯:

自定義指令的開頭:

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

要點是它需要獲取一個對象,用於存儲已checked狀態。 整個過程可以在這里找到:[ coffee / js ]。

在您的指令鏈接功能中,您需要觀察status-stored-in更改,然后重新編譯它,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM