Bhumi Singhal
Bhumi Singhal

Reputation: 8307

Angular expressions take if condition?

I read on the angular guide that the expressions cannot evaluate conditions.

I have a var $scope.a = 2; in by controller

On the partials if I do : ng-show={{a==2}} --- then the ng-show becomes a truthy which is correct

but if I do : ng-if={{a==2}} -- then this does not display

Why the change in behaviour of expressions?

Upvotes: 1

Views: 90

Answers (1)

Tim
Tim

Reputation: 477

ng-if needs an expression, but your binding it to an expression result.

this works:

<div ng-if="1==1">visible</div>
<div ng-if="1==2">not visible</div>

The reason the behaviour differs is because the ng-if has a priority of 600. while the the ng-show has no priority. If i remove the priority from the directive it is able to use the bindings as value, but breaks on something else, so thats why they added the priority.

High prio gets compiled first, so the directive gets executed before the {{expression}} is evaluated to something javascript can understand.

From the angular docs: http://docs.angularjs.org/api/ng/service/$compile

priority

When there are multiple directives defined on a single DOM element, sometimes it is necessary to specify the order in which the directives are applied. The priority is used to sort the directives before their compile functions get called. Priority is defined as a number. Directives with greater numerical priority are compiled first. Pre-link functions are also run in priority order, but post-link functions are run in reverse order. The order of directives with the same priority is undefined. The default priority is 0.

Upvotes: 2

Related Questions