您的指令在ngRepeat
之前運行。當Angular編譯一個ngRepeat
時,它會在它用作標記的代碼中放置特殊註釋。由於您的指令在添加到DOM之前正在運行,因此您的包裝並未包含這些重要的評論。相反,最初創建的HTML看起來像這樣:
<!-- ngRepeat: str in arr track by $index -->
<div class="container">
<textarea my-directive="" ng-repeat="str in arr track by $index" ng-model="arr[$index]" class="ng-scope ng-pristine ng-valid"> </textarea>
</div>
<!-- end ngRepeat: str in arr track by $index -->
當arr
隨後改變了ngRepeat
更新DOM將新的/更新ngRepeat
元素,其中角的標記(評論)是 - 這是您的包裝外面。所以,arr
改變後(和$摘要發生時)你的HTML看起來像這樣:
<!-- ngRepeat: str in arr track by $index -->
<textarea my-directive="" ng-repeat="str in arr track by $index" ng-model="arr[$index]" class="ng-scope ng-pristine ng-valid"> </textarea>
<div class="container"> </div>
<!-- end ngRepeat: str in arr track by $index -->
爲了解決這個問題設置你的指令優先級爲1001以上(ngRepeat有1000優先級):
return {
priority: 1001,
link: function(scope, el, attrs) {.. }
}
現在ngRepeat
將操作DOM第一所以當你的指令包裹元素ngRepeat
評論將被包裹得產生下面的HTML:
<div class="container">
<!-- ngRepeat: str in arr track by $index -->
<textarea my-directive="" ng-repeat="str in arr track by $index" ng-model="arr[$index]" class="ng-scope ng-pristine ng-valid"> </textarea>
<!-- end ngRepeat: str in arr track by $index -->
</div>
和修復您的問題 - updated plunker
我添加刪除NG-repleat,'EL [0] .removeAttribute( 'NG重複')',但我沒有看到任何進展。 – allenhwkim