angular - 无法读取角度 4 中未定义的属性“0”
问题描述
我收到 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>
解决方案
将*ngFor
向上移动到 anng-container
然后将 an*ngIf
放在 上tr
。
<ng-container *ngFor="let row of fsIncomeStatementTable | slice:1">
<tr *ngIf="row" [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>
</ng-container>
从文档:
Angular 是一个不会干扰样式或布局的分组元素,因为 Angular 不会将其放入 DOM 中。
推荐阅读
- django - 来自 postgres 的数据神秘地被删除
- ios - Swift - 两个具有相同公共结构的框架
- swift - Apple ARKit 人脸识别项目 (ARKitFaceExample) 的运行时问题
- c# - 使用 DataBound 事件在 Gridview 中隐藏空列
- azure - az acr list - 从不为服务主体返回任何值
- python - 如何在 fastAPI 中返回图像?
- javascript - 为客户端和 node.js 发布一个库
- php - Heroku PHP 应用程序在本地工作,如果我推送供应商目录,则在部署时工作,但如果我不推送供应商目录,则不会
- mysql - mysql,计算两个表之间的不同值(V2)
- r - 以矢量化方式按位置排列的矢量子集列表