Angular - direct function binding

are there any negative (operational) consequences of binding directly to a function in e. ng-show directive?

<div ng-show="myVm.isVisible()">
....
</div>

// controller snippet (exposed through controllerAs syntax)
function myCtrl (myService, authService) {
  this.isVisible = function isVisible () {
    return (myService.state === 'foo' && authService.isAuthorised);
  }
}

      

this template allows you to hide sometimes complex logic in the template and put the controller (or service) under test, but some developers seem to be concerned about binding the function in directives like ng-show, ng-if, etc.

+3


source to share


1 answer


in fact it is mostly the complexity of your function. angular calls your function every digest cycle and compares the previous value to it. of course it is slower than checking a simple variable. so you can say it has negative consequences. if performance is a big issue for that function (let's say your function is taking too long to compute). you can store the output of your function in a variable, test that variable on the template, and change that variable when needed.



+2


source







All Articles