1

I have the following code:

data-ng-if="(fooCtrl.displayControl(1))

displayControl method return a true or false.

What I found is thatngif is constantly listening for this even after it has been evaluated once. How I found this, was by pointing a break point on the method. After a few F8(in debugger), it will continue as normal.

Why is ngif is executing the method continuously?

Harry
  • 3,031
  • 7
  • 42
  • 67

2 Answers2

4

Angular runs each watcher function on each digest cycle, so this is normal that your function gets checked multiple times. If you don't want this behaviour you can consider one-time binding (Angular 1.3.x).

data-ng-if=":: fooCtrl.displayControl(1)"
dfsq
  • 191,768
  • 25
  • 236
  • 258
2

All your built-in directives will be triggered in every $digest cycle, see more about angular digest cycle here and here

so be aware of using directives like ng-show or ng-if and don't make them listen on a function that does a lot of processing or function that have some loops and that kind of stuff, keep them listening on already determined booleans or simple functions

Community
  • 1
  • 1
amrdruid
  • 951
  • 13
  • 24