有没有一种方法可以使用以下方法订阅多个对象上的事件 $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
提交
取消