angularJS新增事件監聽
阿新 • • 發佈:2019-01-30
angularJS新增監聽事件
如果你想監聽物件是否變化,只需要寫一個事件。
$scope.$watch('setting.enable', function(newValue, oldValue) {
if (newValue === oldValue) { return; }
if (!$scope.setting.enable) {
$scope.setting.isTrue=false;
}
});
如果你想監聽物件內部的屬性值是否變化,則需要寫兩個監聽事件。以下是例子,存下來供參考。
$scope.$watch('setting.enable', function(newValue, oldValue) { if (newValue === oldValue) { return; } if (!$scope.setting.enable) { $scope.setting.isTrue=false; } $scope.$broadcast('valueUpdate', newValue.name); },true); $scope.$on('valueUpdate',function(d,data){ console.info(data); //其中:d為上一個事件,data為newValue.name })