【发布时间】:2018-07-19 20:10:33
【问题描述】:
我收到 TypeError Cannot read property '0' of undefined 。下面是代码。 fsIncomeStatementTable 是 fsIncomeStatementTable 是一个数组数组。我注意到有一行未定义。我如何检查 为那一行?
如果我尝试我的解决方案说无法绑定 ngif,我会收到错误
代码
<tbody>
<tr *ngFor="let row of fsIncomeStatementTable | slice:1" [ngClass]="{'net-cost': row[0] === incomeStatementStyles[0] || row[0] === incomeStatementStyles[1] || row[0] === incomeStatementStyles[2] || row[0] === incomeStatementStyles[3]}">
<td>{{row[0]}}</td>
<td *ngFor="let cell of row | slice:1">{{cell | shortNumberDivideByThousand: 0 | number:'.0-0'}}</td>
</tr>
</tbody>
解决方案
<tbody>
<tr *ngFor="let row of fsIncomeStatementTable | slice:1" [ngClass]="{'net-cost': row[0] === incomeStatementStyles[0] || row[0] === incomeStatementStyles[1] || row[0] === incomeStatementStyles[2] || row[0] === incomeStatementStyles[3]}">
<td>{{row[0]}}</td>
<td *ngif="row !== undefined"> </td>
<td *ngFor="let cell of row | slice:1">{{cell | shortNumberDivideByThousand: 0 | number:'.0-0'}}</td>
</tr>
</tbody>
【问题讨论】:
-
row 本身总是被定义的,因为您没有向集合添加“未定义”。您使用的指令看起来像是对“行”的子属性进行操作,并且子属性变为空。您需要检查正在使用的属性,然后在指令中处理它
标签: angular