All watch-expressions are processed (evaluated) multiple times in every $digest loop.
So, it is a good practice to keep the watch-expression as simple as possible (in terms of computational complexity).
(BTW, using {{...}}
in your view creates a watch.)
If someExpensiveComputation()
is...computationally expensive, you don't want to evaluate it every time, but only when it might produce a different result. It depends on the implementation of the function, but assuming its result relies only on the properties of each poll, then you could watch the poll and call the expensive function whenever it is changed:
.poll
...
{{expensivelyComputedValue}}
.directive('poll', function($rootScope, $modal) {
return {
...
link: function(scope, elem, attrs) {
function someExpensiveComputation(id) { ... }
scope.expensivelyComputedValue = someExpensiveComputation(scope.poll.id);
scope.$watch('poll', function (newValue, oldValue) {
if (newValue === oldValue) { return; }
scope.expensivelyComputedValue = someExpensiveComputation(scope.poll.id);
}, true);
}
}
});
Of course, if the result of the expensive computation depends only on a specific property of poll (and not the entire object), it suffices to watch this property only, thus keeping your watch-expression even more "light-weight". E.g.:
scope.$watch('poll.closed', function (...) {...});