Harry
Harry

Reputation: 3195

ng-if executes method continuously

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?

Upvotes: 1

Views: 817

Answers (2)

dfsq
dfsq

Reputation: 193261

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)"

Upvotes: 4

amrdruid
amrdruid

Reputation: 961

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

Upvotes: 3

Related Questions