ng-content with select attribute work?<ng-content select=".header"></ng-content> <ng-content select=".body"></ng-content>
And this usage:
<app-panel> <div class="header">Title</div> <div class="body">Content</div> <div class="footer">Footer</div> </app-panel>
What will be rendered inside
app-panel?ng-content with select filters projected content.The ng-content elements with select attributes only project matching elements from the content. Elements without a matching selector, like the 'footer' div, are not rendered.
ng-content slotsng-content with attribute selectors?Angular uses the select attribute with CSS selectors to define slots. Attributes like [slot='header'] are valid selectors. The name or slot attributes on ng-content are not valid in Angular.
<ng-content select=".title"></ng-content> <ng-content></ng-content>
And you use it like this:
<app-card> <h1 class="title">Hello</h1> <p>Paragraph</p> </app-card>
But the
h1 appears together with the paragraph in the second slot; the first slot is empty. What is the most likely cause?If the class name on the element does not exactly match the selector in ng-content, it will not be projected into that slot. The default ng-content projects all unmatched content.
ng-content?// child.component.html <div class="child"> <ng-content select=".slot1"></ng-content> <ng-content></ng-content> </div> // parent.component.html <app-child> <div class="slot1">First</div> <div>Second</div> </app-child>
What will be the final rendered HTML inside
app-child?ng-content elements and how selectors filter content.The first ng-content projects elements with class 'slot1'. The second projects all remaining elements. So both divs appear inside the child container in the order of the ng-content tags.
ng-content with selectors instead of multiple inputs?ng-content with select attributes for slot-based composition instead of passing multiple inputs to a component?ng-content with selectors lets you pass rich content like HTML or other components, keeping their structure and styles intact. Inputs are for data, not for passing templates or markup.