慕妹3242003
2019-11-05 15:18:55
有沒有一種方法可以使用以下方法訂閱多個對象上的事件 $watch例如$scope.$watch('item1, item2', function () { });
3 回答

至尊寶的傳說
TA貢獻1789條經驗 獲得超10個贊
從AngularJS 1.1.4開始,您可以使用$watchCollection:
$scope.$watchCollection('[item1, item2]', function(newValues, oldValues){
// do stuff here
// newValues and oldValues contain the new and respectively old value
// of the observed collection array
});

MMTTMM
TA貢獻1869條經驗 獲得超4個贊
$watch 第一個參數也可以是一個函數。
$scope.$watch(function watchBothItems() {
return itemsCombinedValue();
}, function whenItemsChange() {
//stuff
});
如果您的兩個組合值很簡單,則第一個參數通常只是一個角度表達式。例如,firstName和lastName:
$scope.$watch('firstName + lastName', function() {
//stuff
});
添加回答
舉報
0/150
提交
取消