I read an article about some AngularJS traps using scopes , and it says that you should not use a function in expressions, and I understand that an expression can be evaluated every time the structure considers it necessary (and this can happen a lot), and there was It would be inefficient to repeatedly call an expensive function. But what if this function performs only some calculations based on the values already loaded into the region? For example, suppose I have a region with three different properties, and some state is determined by a combination of the values of these properties. I can calculate this state in a function:
$scope.state = function() {
return prop1 && prop2 && prop3;
};
and call this function from the expression. An alternative would be to call a function every time each of the properties is changed so that the state value is cached:
$scope.prop1 = someValue;
$scope.state = getState();
...
$scope.prop2 = someOtherValue;
$scope.state = getState();
...
$scope.prop3 = yetAnotherValue;
$scope.state = getState();
Is it really so bad to call a function directly from an expression in this case? If so, is this the only alternative to caching the computed value, potentially in many different places, or is there some other approach that I am missing?
source
share