3

are there any negative (performance) implications of binding directly to a function in e. g. 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 pattern allows hiding sometimes complex logic in the template and putting in the testable controller (or service) but some developers seem to be worried about binding a function in directives like ng-show, ng-if etc.

1 Answer 1

2

actually it is mostly about complexity of your function. angular calls your function on every digest cycle and compares with it's previous value. of course it is slower than checking a simple variable. so you could say it has negative performance implications. if performance is a big issue on this function (lets say your function takes too much time to compute). you can store output of your function to a variable, check that variable on template and change that variable when it is required to.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.