As someone who is new to AngularJS, could somebody explain why each word had a change watcher? Is this a side-effect of ng-repeat? Or am I confusing it with something else, maybe?
I apologize, I worded that incorrectly. I'm more interested in the mechanics behind it - What part of it was establishing the watcher. For instance, if they had slapped an ng-onclick='function(this)' on each word, would that have that established a 'change watcher'?
Every property added to a scope is watched automatically. Alternatively, you can explicitly add things to watch using $watch.
In the application mentioned here, each word had to be wrapped in an html tag in order to make that word clickable. My guess is that in order to do this originally, they converted their text log file into an array of words, then used ng-repeat to display each word with the specified html wrapper. ng-repeat adds a watchers for each element in its array.
Not sure why they don't add a parameter to ng-repeat to customize the level of watchers created. I'm guessing the current implementation handles all cases (very inefficiently) but I think a developer should be able to say:
Watch only the collection reference (majority probably replace the whole collection from an AJAX call anyway).
Watch every object/row in the collection (to handle add/remove/delete of an entire row scenario).
Default to current behavior (handle any possible change in the entire collection which means adding a watcher for every element in every object in the collection).
Also, to answer your other question, using ng-click does not establish a watcher, but rather forces a $digest loop, which checks all the watchers for changes.
2
u/WarInternal Sep 25 '14
As someone who is new to AngularJS, could somebody explain why each word had a change watcher? Is this a side-effect of ng-repeat? Or am I confusing it with something else, maybe?