2017-09-01 45 views
-1

想要隱藏列表,如果它從json中檢索未定義的。任何人都可以幫忙嗎?如果變量值在角度4中未定義,如何隱藏列表?

HTML file: 

    <ul> 
    <li *ngFor="let item of items | async"><span *ngIf="item?.name">{{item.name}}</span></li> 
    </ul> 

JSON文件:

{ 
    "chat" : { 
     "-Kss3KDwaLDpzAHUKtJN" : { 
      "id" : "11", 
      "name" : "Shanth", 
      "message" : "Hi ..!" 
      }, 
     "-Kss3Kc9c2Ie5IXypqNe" : { 
      "id" : "12", 
      "name" : "", 
      "message" : "Hi .." 
      }, 
     "-KssBwcJhkNCsxUUhsXb" : { 
      "id" : "13", 
      "name" : "Prasanth", 
      "message" : "Hi .." 
      } 
    } 

我越來越像結果如下:

1. Shanth 
2. 
3. Prasanth 

我不想顯示第二個列表。它應顯示爲

1. Shanth 
2. Prasanth 

回答

-1

試試這個

<ul> 
    <ng-container *ngFor="let item of items | async"> 
    <li *ngIf="item.name">...</li> 
    </ng-container> 
</ul> 
+0

引發錯誤「無法找到類型爲'object'的不同支持對象'[object Object]'。NgFor僅支持綁定到Iterables,比如」數組「 –

+0

對不起,我認爲我可以寫得很快。在ng容器中使用'* ngFor =「let item | async」' – trichetriche

+0

我剛剛編輯了我的答案,以便您可以看到它 – trichetriche

-1

使用,如果條件對李,而不是跨

<li *ngFor="let item of items | async" [style.display]="item.name?'block':'none'"><span >{{item.name}}</span></li> 

您可以如用NG-容器作爲trichetriche的回答在answers.Please看的人提了。

+0

將錯誤引發爲「一個元素上不能有多個模板綁定」。 –

+0

請查看我對該錯誤的回答。 – trichetriche

+0

好mybad,沒有測試它..請檢查我更新的代碼.. –

0

這將解決你的問題。

<ul> 
    <div *ngFor="let item of items"> 
    <li *ngIf="item.name"><span>{{item.name}}</span></li> 
    </div> 
</ul>