Javascript $watch不使用美元($)触发变量
我有一块手表找东西。当对象的属性发生更改时,监视将启动,除非属性名称以美元($)开头 考虑以下代码:Javascript $watch不使用美元($)触发变量,javascript,angularjs,angularjs-scope,angularjs-watch,Javascript,Angularjs,Angularjs Scope,Angularjs Watch,我有一块手表找东西。当对象的属性发生更改时,监视将启动,除非属性名称以美元($)开头 考虑以下代码: $scope.data = {}; $scope.$watch('data', function (newValue, oldValue) { console.log('fired', newValue); }, true); 以及html: <div> this will fire the event <input type="text" ng-m
$scope.data = {};
$scope.$watch('data', function (newValue, oldValue) {
console.log('fired', newValue);
}, true);
以及html:
<div>
this will fire the event
<input type="text" ng-model="data.x" />
</div>
<div>
this won't
<input type="text" ng-model="data.$" />
</div>
这将触发事件
这不会
你可以在这把小提琴上看到它:
这对我来说非常重要,因为我必须使用带有$的属性名进行筛选。AngularJs具有“$watchCollection”功能,可以观察集合(即数组或对象)中的更改
请尝试上面的片段,因为您想观看一个对象。希望这能有所帮助。AngularJs具有“$watchCollection”功能,可用于观察集合(即数组或对象)中的更改
请尝试上面的片段,因为您想观看一个对象。希望这有帮助。这似乎回答了您的问题这似乎回答了您的问题太棒了,谢谢!太棒了,谢谢!
$scope.$watchCollection('data', function (newValue, oldValue) {
console.log('fired', newValue);
});