ng-if is not working as expected.

Swati44
Kilo Expert

Hi All,

<td ng-if="item.response_code == '300'"> <button id="btnIncident" name="submit" ng-click="c.remove(item.name)" type="submit">Remove</button></td>
</tr>

 

The above line is not working as expected, Remove button is not visible all time, irrespective of response code.And it is visible and working as expected if i remove 'ng-if="item.response_code == '300'"'.

How to compare the item.response_code value?

 

Thanks & Regards,

Swathi.

1 ACCEPTED SOLUTION

I forget sometimes how javascript works. This should do the trick:

item.response_code === '300'

Depending upon the situation javascript interprets '300' and 300 as being equal or the same. Whereas if you use the equality where it checks the type as well then it will prove to be actually an integer vs a string version of 300.

 

But to answer your other question how to compare without ng-if:

Simply place the expression in the markup:

<div> {{ item.response_code === '300' }} </div> 

That syntax is what is doing the comparison. ngIf is just looking for a result.

View solution in original post

13 REPLIES 13

ChrisBurks
Mega Sage

Hi Swathi,

 

You could print out the value of item.response_code within the rendered view:

<td> {{item.response_code }}<button id="btnIncident" name="submit" ng-click="c.remove(item.name)" type="submit">Remove</button></td>
</tr>

or print out the comparison to ensue the output is what you're expecting:

<td> {{ item.response_code == '300' }} <button id ...

 

But just for clarity if ng-if's comparison is true it will display the markup. If it's false it won't display.

 

 

 

 

Hi Chris,

Printed value is giving 300

What about the comparison? Did you print out the comparison result in the html:{{ item.response_code == '300' }}?

What was the result? If the value of item.response_code showed '300' and the result of the comparison is false then you might want to also check the typeof of the value.

Hi Chris,

 

It shows 300==300. How to compare without ng-if?