Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

*ngIf and *ngFor on <td></td> element [duplicate]

I have a situation where I need *ngIf and *ngFor directive on the same element. I found lot of answers on the stackoverlow but none for the this type of situation.

I have a table in which I'm looping through the array of objects and dynamically write cells in header:

 <table border="1" width="100%">         <thead>             <tr>                 <td *ngFor="let item of headerItems" *ngIf="item.visible">{{ item?.name }}</td>             </tr>         </thead>         <tbody>             ...         </tbody>     </table> 

I want to show/hide if object contains visible set to true. How can I achieve this?

like image 507
Igor Janković Avatar asked Nov 29 '16 08:11

Igor Janković


People also ask

Can we use ngFor and ngIf on the same element?

Use ngFor and ngIf on same element It's very common scenario where we want to repeat a block of HTML using ngFor only when a particular condition is true. i.e., if ngIf is true.So in this case to use *ngIf and *ngFor on same element, place the *ngIf on a parent element that wraps the *ngFor element.

What is the difference between ngFor and ngIf?

NgIf conditionally displays items by adding or removing them from the DOM depending on the condition. NgFor renders a list of items from iterable objects.

What is the use of ngIf and ngFor?

*ngIf and *ngFor can help you keep your Angular code clean, simple, and effective.

Can we use ngIf and Ngclass together?

javascript - ng-if and ng-class-even/odd doesn't work well together and won't get expected outout - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.


1 Answers

You can use the <ng-container> helper element for that.

<ng-container *ngFor="let item of headerItems" >  <td *ngIf="item.visible">{{ item?.name }}</td> </ng-container> 

It is not added to the DOM.

like image 115
Günter Zöchbauer Avatar answered Oct 11 '22 20:10

Günter Zöchbauer