How to check empty object in angular 2 template using *ngIf

前端 未结 7 1180
逝去的感伤
逝去的感伤 2020-12-04 16:26

I want to check if my object is empty dont render my element, and this is my code:


                      
相关标签:
7条回答
  • 2020-12-04 17:10

    Just for readability created library ngx-if-empty-or-has-items it will check if an object, set, map or array is not empty. Maybe it will help somebody. It has the same functionality as ngIf (then, else and 'as' syntax is supported).

    arrayOrObjWithData = ['1'] || {id: 1}
    
    <h1 *ngxIfNotEmpty="arrayOrObjWithData">
      You will see it
    </h1>
    
     or 
     // store the result of async pipe in variable
     <h1 *ngxIfNotEmpty="arrayOrObjWithData$ | async as obj">
      {{obj.id}}
    </h1>
    
     or
    
    noData = [] || {}
    <h1 *ngxIfHasItems="noData">
       You will NOT see it
    </h1>
    
    0 讨论(0)
提交回复
热议问题