admin管理员组文章数量:1331650
fiddle
I have a list, and I'd like to be notified whenever it changes. A simple $watch
expression isn't working, which I'm guessing is because angular is checking for referential equality, not structural equality.
<html ng-app>
<head>
</head>
<body ng-controller="Root">
<h1>Base Angular Fiddle</h1>
Times changed: {{timesChanged}}
<ul>
<li ng-repeat="name in names">{{name}}</li>
<li><button ng-click="names.push('another name')">Add</button></li>
</ul>
</body>
</html>
function Root($scope) {
$scope.timesChanged = 0;
$scope.names = ['foo', 'bar', 'baz'];
$scope.$watch('names', function() {
$scope.timesChanged++;
});
}
What I'm hoping will happen is that the callback for 'names'
is called each time that I call names.push()
. Is there a workaround that's remended for this? Or am I just not using $watch
correctly?
fiddle
I have a list, and I'd like to be notified whenever it changes. A simple $watch
expression isn't working, which I'm guessing is because angular is checking for referential equality, not structural equality.
<html ng-app>
<head>
</head>
<body ng-controller="Root">
<h1>Base Angular Fiddle</h1>
Times changed: {{timesChanged}}
<ul>
<li ng-repeat="name in names">{{name}}</li>
<li><button ng-click="names.push('another name')">Add</button></li>
</ul>
</body>
</html>
function Root($scope) {
$scope.timesChanged = 0;
$scope.names = ['foo', 'bar', 'baz'];
$scope.$watch('names', function() {
$scope.timesChanged++;
});
}
What I'm hoping will happen is that the callback for 'names'
is called each time that I call names.push()
. Is there a workaround that's remended for this? Or am I just not using $watch
correctly?
- You may have posted the wrong fiddle version. There is no code in the fiddle you posted. – Kyeotic Commented Dec 11, 2012 at 22:50
- Your fiddle doesn't have this code - can you update? – Roy Truelove Commented Dec 11, 2012 at 22:51
- Yep, I definitely posted the wrong version - sorry about that. – Nick Heiner Commented Dec 11, 2012 at 23:12
2 Answers
Reset to default 9You are right, angular checks for reference by default, for perfomance reasons. The usage of reference vs equality depends on the value of the $watch function's third argument, as you can see in the docs. Just set it to true (it is false by default) and your fiddle will work - here it is, working.
the change:
$scope.$watch('names', function() {
$scope.timesChanged++;
}, true);
EDIT - Performance implications:
Comparing two arrays/objects can obviously be very inefficient (hence the check by reference angular makes by default), depending on the size and plexity of your variables. There is an alternate method - watching the length of the array. This has obvious limitations - changing one element no longer triggers the $watch function - but can be immensely faster. Here is an updated fiddle!
Just a quick addition:
If you want to watch for more special things you can do it like so:
$scope.$watch(function() {
// Define what you are watching for
// For example convert your array into a string
return String($scope.names);
}, function(newValue, oldValue) {
// Do something cool.
});
This is definitely not as fast as names.length
but you could play around and find a sophisticating solution.
If anyone is interested I played around a bit with fast hashing that uses a modified Duff's device: http://jsfiddle/flek/mfKT6/1/ If you are dealing with massive data this hashing method could also be updated to be error responsive (like hash only every 2nd or 3rd character)
本文标签: javascriptHow to watch for changes to a listStack Overflow
版权声明:本文标题:javascript - How to $watch for changes to a list? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742266651a2443525.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论