61

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

6
  • are you confusing the test in your ng-repeat with a test variable on your scope? otherwise, you don't need to check for it because test isn't going to render if current is empty. Commented May 9, 2014 at 3:28
  • i changed the example a bit. now i have test.view == null Commented May 9, 2014 at 3:29
  • I don't understand.. your code shows test.view == null but the context of your problem below says test == null doesn't work. Please clarify your question. Commented May 9, 2014 at 3:31
  • stackoverflow.com/questions/18830035/… Commented May 9, 2014 at 3:31
  • also, null doesn't evaluate to falsey like you probably expect. Commented May 9, 2014 at 3:33

4 Answers 4

59

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

<span ng-if="!test">null</span>
Sign up to request clarification or add additional context in comments.

4 Comments

Add the code to your example please, so developers can quickly see the code answer :-)
In your example there might be a problem if the array has 0 in it... :)
Thanks! If you don't want to include zeroes, then ng-if="!test && test != 0" would work. I'm not sure if there's a better way, though.
the issue with this is that " $scope.current = [1, null, 0];" will render "0 and null"
28

See the correct way with your example:

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

Regards, Nicholls

4 Comments

tried this in angular 2 using *ngIf but its not working
it's only to Angular 1 :)
This won't handle a zero value
@BryantJackson you're right, with numbers he needs to validate the type too
2

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

Comments

1

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>

Comments

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.