无法获取角度 6 中未定义或空引用的属性'propertyName'



我将获得以下问题

无法获得未定义或null参考的属性'senderDetails'

我在Angular 6

中使用以下代码
<tr *ngIf="itemList.senderDetails?.length !=null">
<th scope="row">Sender Owner</th>
<td><strong>ID</strong>{{itemList.senderId}} |<strong>Name</strong>{{itemList.sender}}</td>
</tr>

上面的代码在Chrom中运行良好,但在IE Edge中丢弃了未定义或无null引用的错误

只需尝试以下

<tr *ngIf="itemList?.senderDetails?.length">
<th scope="row">Sender Owner</th>
<td><strong>ID</strong>{{itemList.senderId}} |<strong>Name</strong>{{itemList.sender}}</td>
</tr>

这是因为您的项目列表始于一开始。你可以

  1. 初始化您的项目列表

    itemlist = {};

    ,或者如果您正在使用类来存储项目列表数据

    itemList:itemList = new ItemList((;

  2. 在外部元素中添加第二个 *ngif,因此在item列表不确定时,内部代码将不会执行 *ngif =" itemList!= undefined"

这是因为在呈现页面(视图(时,itemList对象为null或不确定。如果您从异步调用(API(获取项目列表的数据,则会出现此类问题。因此,我们应该始终检查对象是null还是未定义,然后如果不是零,则可以访问其属性。在您的状态下,您可以将以下代码用于"如果stattement"。

<tr *ngIf="itemList && (itemList.senderDetails?.length !=null")>

最新更新