AngularJS – Why does $scope.$apply affect other scopes and how it affects performance?

March 29, 2014

as you can see from Angular’s $apply pseudo-code: function $apply(expr) { try { return $eval(expr); } catch (e) { $exceptionHandler(e); } finally { $root.$digest(); } } Internally angular does the following: $scope.$apply = $rootScope.$digest //+ some error handling and since $scope.$apply uses $rootScope, it affects all its descendants by dirty-checking EVERY data-bound objects, it’s crazy,...
one comment

AngularJS – $watch for changes in specific object property when watching an array

March 26, 2014

When deep $watch is needed on an array, but not for the entire object, you can strip off irrelevant data, this way you make the comparison much faster. Example: (solution by Karl Seamon) $scope.$watch(function($scope) { return $scope.listOfBigObjects. map(function(bigObject) { return bigObject.foo. fieldICareAbout; }); }, myHandler, true); this way you watch only what you need. Live example for watching only for "name" property: hope you’ll find this post helpful.
17 comments