我收到TypeError无法读取undefined的属性“ 0”。下面是代码。 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>
答案 0 :(得分:0)
将*ngFor
移到ng-container
上,然后将*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>
来自docs:
Angular是一个分组元素,它不会干扰样式或布局,因为Angular不会将其放在DOM中。
答案 1 :(得分:0)
您尝试在此处访问您的行
<td>{{row[0]}}</td>
即使未定义。
您的代码应评分为:
<td *ngif="row !== undefined">{{row[0]}}</td>
<td *ngif="row === undefined"> </td>