I want to render my rather simple component always with a default <ng-content> and optionally with a named ng-content. If that named ng-content exists, it should be wrapped by additional html. I did find a a solution, but annoyingly I need two references for it:
app.component.html (where it get's used)
<app-header> A minimal header </app-header> <hr> <app-header> A full header <h2 #two two>a subhead</h2> </app-header> my component
import { Component, OnInit, ContentChild, ElementRef } from '@angular/core'; @Component({ selector: 'app-header', templateUrl: './header.component.html' }) export class HeaderComponent { @ContentChild('two', {static: false}) twoRef: ElementRef; } the header.component.html
<h4>default content</h4> <ng-content> </ng-content> <h4>named contents</h4> <div *ngIf="twoRef"> <b style="color: purple"> <ng-content select="[two]"></ng-content> </b> </div> <ng-template [ngIf]="twoRef">TWO exists B</ng-template> Result is as expected, but I dislike the two attributes I need:
<h2 #two two>