简体   繁体   English

如何在角度5中有条件地向表中添加行

[英]How to conditionally add rows to table in angular 5

I am new to Angular 5. I have an array of objects in my app.component.ts . 我是Angular 5的app.component.ts ,我的app.component.ts有一个对象数组。 I want to use both *ngFor and *ngIf to create rows of a table and apply condition at the same time. 我想同时使用*ngFor*ngIf来创建表的行并同时应用条件。 Basically something like 基本上像

<tr *ngFor="let row of rows" *ngIf="row['category']==='M'"><td>...</td><td>...</td></tr>

This is however not allowed and an error is displayd: 但是,这是不允许的,并显示错误:

Can't have multiple template bindings on one element. 一个元素不能有多个模板绑定。

How to achieve this? 如何实现呢?

Use ng-container ( documentaion link ) for your ngFor . ngFor使用ng-container文档链接 )。

<ng-container *ngFor= "let row of rows">
   <tr  *ngIf="row['category']==='M'"><td>...</td><td>...</td></tr>
</ng-container>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM