Simer Simer - 1 year ago 70
AngularJS Question

ng-if not behaving as expected

I have an angular expression {{userInfo.activities.ids}}, it is an array which I am sending to angular through php.
Now I need to check a value in this array so I am using this :

[[userInfo.activities.ids.indexOf(1) != -1]]
and It returns true or false.
I wanted to apply ng-if on basis of it's value.
Therefore I wrote:

<input ng-if="{{userInfo.activities.ids.indexOf(1) != -1}} == true" checked type="checkbox">
<input ng-if="{{userInfo.activities.ids.indexOf(1) == -1}} == true" type="checkbox">

But ng-if is not verifying condition and therefore none of the input gets displayed.

Please suggest what method I can use instead of this?
For separating my template from angular expression I have used [[]] interpolates.
I am using angular along with laravel 5.2 php.

Answer Source

Remove your braces in the ng-if condition,

If you are using the scope variables in the angular models such as ng-if, ng-model, ng-class etc, there is no need to use the interpolation(curly-braces).

Also as your expression evaluates either true or false, you no need to again check == true condition

<input ng-if="((userInfo.activities.ids.indexOf(1)) != -1)" checked type="checkbox">
<input ng-if="((userInfo.activities.ids.indexOf(1)) == -1)" type="checkbox">

Here is a reference

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download