how to check for null with a ng-if values in a view with angularjs?

JavascriptAngularjsIf StatementIsnull

Javascript Problem Overview


i have this situation

<div ng-repeat="test in current">
	<div ng-if="test.view == null">
		<i class="icon ion-checkmark"></i>
	</div>
</div>

but test.view== null doesn't work, neither just checking for test.view or test.view == ''

any ideas?

thanks

edit:

in the loop, sometimes test.view, has a value sometimes is NULL if i do:

<div ng-if="!test.view">1</div>
<div ng-if="test.view">2</div>

i will only see 1

Javascript Solutions


Solution 1 - Javascript

You should check for !test, here is a fiddle showing that.

<span ng-if="!test">null</span>

Solution 2 - Javascript

See the correct way with your example:

<div ng-if="!test.view">1</div>
<div ng-if="!!test.view">2</div>

Regards, Nicholls

Solution 3 - Javascript

You can also use ng-template, I think that would be more efficient while run time :)

<div ng-if="!test.view; else somethingElse">1</div>
<ng-template #somethingElse>
    <div>2</div>
</ng-template>

Cheers

Solution 4 - Javascript

Here is a simple example that I tried to explain.

<div>
    <div *ngIf="product">     <!--If "product" exists-->
      <h2>Product Details</h2><hr>
      <h4>Name: {{ product.name }}</h4>
      <h5>Price: {{ product.price | currency }}</h5>
      <p> Description: {{ product.description }}</p>
    </div>

    <div *ngIf="!product">     <!--If "product" not exists-->
       *Product not found
    </div>
</div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPatrioticcowView Question on Stackoverflow
Solution 1 - JavascriptLucas PolonioView Answer on Stackoverflow
Solution 2 - JavascriptjdnichollscView Answer on Stackoverflow
Solution 3 - JavascriptTanzeelView Answer on Stackoverflow
Solution 4 - JavascriptArsman AhmadView Answer on Stackoverflow