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

后端 未结 4 582
难免孤独
难免孤独 2021-02-06 20:35

i have this situation

相关标签:
4条回答
  • 2021-02-06 21:00

    See the correct way with your example:

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

    Regards, Nicholls

    0 讨论(0)
  • 2021-02-06 21:21

    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

    0 讨论(0)
  • 2021-02-06 21:23

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

    <span ng-if="!test">null</span>
    
    0 讨论(0)
  • 2021-02-06 21:24

    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>
    
    0 讨论(0)
提交回复
热议问题