Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I want to use both *ngFor and *ngIf to create rows of a table and apply condition at the same time. 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?

like image 213
Abdul-Baqi Sharaf Avatar asked Oct 19 '25 03:10

Abdul-Baqi Sharaf


1 Answers

Use ng-container(documentaion link) for your ngFor.

<ng-container *ngFor= "let row of rows">
   <tr  *ngIf="row['category']==='M'"><td>...</td><td>...</td></tr>
</ng-container>
like image 51
Sajeetharan Avatar answered Oct 20 '25 17:10

Sajeetharan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!