Skip to content

Commit 523f71a

Browse files
authored
docs(cdk/portal): update portal docs (#19869)
Add the DomPortal example in the cdk-portal-overview-example component and update the Portal markdown documentation adding TemplatePortal and DomPortal.
1 parent 2eff545 commit 523f71a

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

src/cdk/portal/portal.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ The `portals` package provides a flexible system for rendering dynamic content i
33
### Portals
44
A `Portal` is a piece of UI that can be dynamically rendered to an open slot on the page.
55

6-
The "piece of UI" can be either a `Component` or a `TemplateRef` and the "open slot" is
6+
The "piece of UI" can be either a `Component`, a `TemplateRef` or a DOM element and the "open slot" is
77
a `PortalOutlet`.
88

99
Portals and PortalOutlets are low-level building blocks that other concepts, such as overlays, are
@@ -55,7 +55,43 @@ portals, it must be included in the `entryComponents` of its `NgModule`.
5555

5656
Usage:
5757
```ts
58-
this.userSettingsPortal = new ComponentPortal(UserSettingsComponent);
58+
ngAfterViewInit() {
59+
this.userSettingsPortal = new ComponentPortal(UserSettingsComponent);
60+
}
61+
```
62+
63+
##### `TemplatePortal`
64+
You can create a `TemplatePortal` from an `<ng-template>`. `TemplatePortal` allows you to take Angular content within one template and render it somewhere else.
65+
66+
Usage:
67+
```html
68+
<ng-template #templatePortalContent>Some content here</ng-template>
69+
```
70+
71+
```ts
72+
@ViewChild('templatePortalContent') templatePortalContent: TemplateRef<unknow>;
73+
74+
ngAfterViewInit() {
75+
this.templatePortal = new TemplatePortal(
76+
this.templatePortalContent,
77+
this._viewContainerRef
78+
);
79+
}
80+
```
81+
82+
##### `DomPortal`
83+
You can create a `DomPortal` from any native DOM element. `DomPortal` allows you to take any arbitrary DOM content and render it somewhere else. `DomPortal` moves content _as is_, so elements with Angular features like bindings or directives may no longer update if moved via `DomPortal`.
84+
85+
Usage:
86+
```html
87+
<div #domPortalContent>Some content here</div>
88+
```
89+
90+
```ts
91+
@ViewChild('domPortalContent', {static: false}) domPortalContent: ElementRef<HTMLElement>;
92+
ngAfterViewInit() {
93+
this.domPortal = new DomPortal(this.domPortalContent);
94+
}
5995
```
6096

6197

src/components-examples/cdk/portal/cdk-portal-overview/cdk-portal-overview-example.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ <h2>The portal outlet is below:</h2>
66

77
<button (click)="selectedPortal = componentPortal">Render component portal</button>
88
<button (click)="selectedPortal = templatePortal">Render template portal</button>
9+
<button (click)="selectedPortal = domPortal">Render DOM portal</button>
10+
11+
<div #domPortalContent>Hello, this is a DOM portal</div>
Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import {Component, AfterViewInit, TemplateRef, ViewChild, ViewContainerRef} from '@angular/core';
2-
import {ComponentPortal, Portal, TemplatePortal} from '@angular/cdk/portal';
1+
import {ComponentPortal, DomPortal, Portal, TemplatePortal} from '@angular/cdk/portal';
2+
import {
3+
AfterViewInit,
4+
Component,
5+
TemplateRef,
6+
ViewChild,
7+
ViewContainerRef,
8+
ElementRef
9+
} from '@angular/core';
310

411
/**
512
* @title Portal overview
@@ -10,21 +17,28 @@ import {ComponentPortal, Portal, TemplatePortal} from '@angular/cdk/portal';
1017
styleUrls: ['cdk-portal-overview-example.css'],
1118
})
1219
export class CdkPortalOverviewExample implements AfterViewInit {
13-
@ViewChild('templatePortalContent') templatePortalContent: TemplateRef<any>;
20+
@ViewChild('templatePortalContent') templatePortalContent: TemplateRef<unknown>;
21+
@ViewChild('domPortalContent', {static: true}) domPortalContent: ElementRef<HTMLElement>;
22+
1423
selectedPortal: Portal<any>;
1524
componentPortal: ComponentPortal<ComponentPortalExample>;
1625
templatePortal: TemplatePortal<any>;
26+
domPortal: DomPortal<any>;
1727

18-
constructor(private _viewContainerRef: ViewContainerRef) {}
28+
constructor(private _viewContainerRef: ViewContainerRef) { }
1929

2030
ngAfterViewInit() {
2131
this.componentPortal = new ComponentPortal(ComponentPortalExample);
22-
this.templatePortal = new TemplatePortal(this.templatePortalContent, this._viewContainerRef);
32+
this.templatePortal = new TemplatePortal(
33+
this.templatePortalContent,
34+
this._viewContainerRef
35+
);
36+
this.domPortal = new DomPortal(this.domPortalContent);
2337
}
2438
}
2539

2640
@Component({
2741
selector: 'component-portal-example',
2842
template: 'Hello, this is a component portal'
2943
})
30-
export class ComponentPortalExample {}
44+
export class ComponentPortalExample { }

0 commit comments

Comments
 (0)