Performance: ng-content for slot-based composition
This affects rendering speed and layout stability by controlling how content is projected into components.
<div> <ng-content select="[header]"></ng-content> <ng-content select="[main]"></ng-content> </div>
<div> <ng-content></ng-content> <ng-content></ng-content> </div>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple unnamed ng-content | Duplicates projected nodes | Multiple reflows | High paint cost due to layout shifts | [X] Bad |
| Named ng-content slots with select | Projects nodes once correctly | Single reflow | Lower paint cost with stable layout | [OK] Good |
ng-content in Angular components?ng-content roleng-content is used to project or insert external HTML content into a component's template.ng-content in Angular?select attribute on ng-content to create named slots by CSS selectors.select with a CSS class selector, which is correct syntax.<div> <ng-content select=".title"></ng-content> <ng-content select=".body"></ng-content> </div>
<my-comp> <h1 class="title">Hello</h1> <p class="body">Welcome!</p> </my-comp>
<my-comp>?ng-content select=".title" inserts elements with class title, and select=".body" inserts elements with class body..title content first, then .body content, so the output matches that order.<div> <ng-content select="header"></ng-content> <ng-content select=".body"></ng-content> </div>
<my-comp> <div class="header">Top</div> <div class="body">Content</div> </my-comp>
ng-content uses select="header", which looks for elements named <header>, not class header.<div class="header">, which does not match the selector header (element selector). It should be .header to match the class.ng-content to support this slot-based composition?select with CSS selectors to define named slots in ng-content.select with class selectors, matching Angular's pattern. Options B, C, and D use invalid selectors or attributes (name, slot) not supported by Angular.header, section, footer), improving accessibility and structure.Pass the test and this pattern moves unknown