这是我的代码
<tr *ngFor="let sample of data; let i = index" [attr.data-index]="i">
<ng-container *ngIf="sample .configuration_type == 1; then thenBlock; else elseBlock"></ng-container>
<ng-template #thenBlock>
<td>{{sample.item_number}}</td>
<td>{{sample.make}}</td>
<td>{{sample.model}}</td>
</ng-template>
<ng-template #elseBlock>
<td>{{sample.serial_number}}</td>
<td>{{sample.capacity}}</td>
<td>{{sample.model}}</td>
</ng-template>
</tr>
错误显示为
无法绑定(bind)到“ngIfThen”,因为它不是“ng-container”的已知属性。 1. 如果“ng-container”是 Angular 组件并且具有“ngIfThen”输入,则验证它是否是该模块的一部分。 2. 如果“ng-container”是 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以抑制此消息。 ("gFor="let 数据样本;let i = 索引"[attr.data-index]="i">
最佳答案
正如错误所示,您不能在同一标签上使用 ngFor 和 ngIf,而是使用 ng-container 来执行此操作,
<tr *ngFor="let sample of data; let i = index" [attr.data-index]="i">
<ng-container *ngIf="sample.configuration_type === 1">
<td>{{sample.item_number}}</td>
<td>{{sample.make}}</td>
<td>{{sample.model}}</td>
<td>{{bucket.class}}</td>
<td>{{bucket.capacity}}</td>
</ng-container>
</tr>
关于javascript - 如何在 Angular 2 中使用 ngIf-else 中的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45566838/