首页 > 解决方案 > 在 ngFor 中测试 null

问题描述

我想在不为空payment.invoice.reference时显示。invoice

我试过这个:

<tr *ngFor="let payment of payments; index as i">
  <td>
    <span *ngIf="payment.invoice">{{payment.invoice.reference}}</span>
    <span *ngIf="!payment.invoice">Notinh</span>
  </td>
</tr>

我试过这个:

<tr *ngFor="let payment of payments; index as i">
  <td>{{payment?.invoice.reference}}</td>
</tr>

但那是行不通的。

任何的想法 ?

标签: angulartypescript

解决方案


你已经在付款了吗?,它与您需要在发票后添加的内容相同。它被称为安全导航算子,

 <td>{{payment?.invoice?.reference}}</td>

或者,您也可以使用*ngIf指令


推荐阅读