Professional Documents
Culture Documents
Angular - Content Projection
Angular - Content Projection
reusable components.
content you want to use inside another component. For example, you
another component.
1. Create a component.
to display a message.
content-projection/src/app/zippy-basic/zippy-basic.component.ts
{ } '@angular∕core';
@Component({
selector: 'app-zippy-basic',
: `
<h2>Single-slot content projection<∕h2>
<ng-content><∕ng-content>
`
})
{}
now project their own message into the component. For example:
content-projection/src/app/app.component.html
selector that determines which content goes into that slot. This pattern
must specify where you want the projected content to appear. You
1. Create a component.
elements.
content-projection/src/app/zippy-multislot/zippy-
multislot.component.ts
{ } '@angular∕core';
@Component({
selector: 'app-zippy-multislot',
: `
<h2>Multi-slot content projection<∕h2>
Default:
<ng-content><∕ng-content>
Question:
<ng-content select="[question]"><∕ng-content>
`
})
{}
Content that uses the question attribute is projected into the <ng-
question
Is content projection cool?
conditionally render.
an ngIf statement.
times as you want. Angular will not initialize the content of an <ng-
content-projection/src/app/example-zippy.template.html
[ngTemplateOutlet]="content.templateRef"
content-projection/src/app/example-zippy.template.html
*ngIf="expanded" [id]="contentId"
[ngTemplateOutlet]="content.templateRef"
content-projection/src/app/app.component.html
appExampleZippyContent
It depends on what you do with it.
The <ng-template> element de�nes a block of content that a
method createEmbeddedView() .
TemplateRef instance.
content-projection/src/app/example-zippy.component.ts
@Directive({
selector: '[appExampleZippyContent]'
})
{
( templateRef:
<unknown>) {}
}
provides the logic that Angular will use when it encounters that
@ContentChild( ) content!:
;
elements.
question
Is content projection cool?
For example, the content you want to project might be a child of another
content-projection/src/app/app.component.html
ngProjectAs="[question]"
Is content projection cool?
REMINDER