改變重新指令說你有這樣的事情:如何當範圍在AngularJS
<div mydirective>
{{boundtoscope}}
</div>
而你要mydirective要對$ scope.boundtoscope變化應用。你如何指導Angular重新申請指令?
改變重新指令說你有這樣的事情:如何當範圍在AngularJS
<div mydirective>
{{boundtoscope}}
</div>
而你要mydirective要對$ scope.boundtoscope變化應用。你如何指導Angular重新申請指令?
在你指導的鏈接功能$觀看範圍特性:
myApp.directive('mydirective', function() {
return {
link: function (scope, element, attrs) {
scope.$watch('boundtoscope', function(newValue) {
alert('boundtoscope changed');
// do something here
});
}
}
});
如果boundtoscope
是一個數組或對象圖,你要尋找的數組/對象中更改的項目,設置objectEquality argument到true
執行「淺」的手錶(即,比較是否相等陣列/對象,而不是供參考):
scope.$watch('boundtoscope', function(newValue) {
alert('boundtoscope changed')
}, true); // "shallow" compare
角1.2增加了對這種新的方法,$watchCollection:
scope.$watchCollection('boundtoscope', function(newValue) {
alert('boundtoscope changed')
}); // also does a "shallow" compare
非常感謝你。正是我所需要的,而且從文檔中並不明顯。 –
我完全打算做一堆複雜的事情,比如實際上覆制和刪除元素,然後將它扔回去。這很簡單。謝謝! –
據我所知,最好是在指令的控制器中而不是在鏈接器中執行'$ scope'這樣的操作。鏈接器更多用於一次性DOM操作。這是我在閱讀[本書](http://www.manning.com/bford/)以及關於該主題的一些關於互聯網的一般閱讀之後對事物的解釋。我很高興能夠改正! – alastairs
[這是NG-綁定指令如何做。](https://github.com/angular/angular.js/blob/master/src/ng/directive/ngBind.js#L55) – laggingreflex