1

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?

1 Answer 1

2

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.

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

14 Comments

actually ng-shows expect expression as well I think you or me misunderstand the question... for me the question why it is not working with ng-if while it is working with ng-show?
@wickY26 - Xcatly that is my question. Why is it working for ng-show and not ng-if
Yeah, i misunderstood. I found it though, so changed my answer.
Thank you. So what is the correct way {{}} or no {{}}
And why does the documentation state that expressions do not do conditional thingys
|

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.