LJ Wadowski
LJ Wadowski

Reputation: 6720

"destroy" ng-if once expression is true

I am wondering if is possible to unbind/destroy ng-if once its value is true?

I created tree structure with recurrence directive and each of branches has <div ng-if="visible"> which keeps tracking if element needs to be rendered. The problem is that the solution increase number of watchers because every ng-if creates new one. Once the ng-if expression became true it won't change so watch can be removed, is there any way to "destroy" ng-if in that case?

Upvotes: 2

Views: 1152

Answers (2)

Sudheer
Sudheer

Reputation: 2985

if using angular 1.3+ you can do

<div ng-if="::visible">

Which will remove the expression from angular watchlist and essentially no watches on it. Use this for any single binded expressions in your app. Keeps your watch count low and digest cycles faster.

Upvotes: 6

Roberto Linares
Roberto Linares

Reputation: 2225

You could use bindonce library, which happens to have a bo-if = "condition" directive attribute, which is described as:

equivalent to ng-if but doesn't use watchers

So is somewhat similar of what you want to achieve.

Upvotes: 2

Related Questions