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
})