Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular difference between ng-container and div

Since I can do *ngif on both why even use ng-container and just use div everywhere instead? I know it has something to do with the dom not being as cluttered but I actually want to know if that is true and how a bigger dom impacts the app.

like image 469
coloradoman Avatar asked Dec 22 '25 14:12

coloradoman


1 Answers

ng-container doesn't render any HTML, while a div adds a node to the DOM. If you have a larger DOM, you will negatively impact performance and memory usage.

Let's say you have a list of 500 people and you want to display their name, so you do

<div *ngFor="let person of people">
  <div *ngIf="person.visible">
    <span>{{ person.name }}</span>
  </div>
</div>

That adds 500 elements to the DOM, affecting performance. Now, if you try the same thing with ng-container

<div *ngFor="let person of people">
  <ng-container *ngIf="person.visible">
    <span>{{ person.name }}</span>
  </ng-container>
</div>

That won't add anything unecessary to the DOM

like image 125
vmHernandes Avatar answered Dec 24 '25 04:12

vmHernandes



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!