When working with web frameworks we are often constrained to rendering content in a specific part of the DOM in which the component we are working on is rendered, and changing this ordering can often be a lot of work. In order to simplify this, most frameworks provide a concept of a Portal that allows us to define some content in one part of our app and render it elsewhere.
For the sake of our example, I would like to be able to write some markup that looks like this:
And have it render at some other place in the DOM. Since we’re using Angular we’ll use a ng-template to denote where we ouput our content, and we can programatically render stuff into this template with Angular
To create this kind of API we need to define the following:
Portal Content
A component that defines the content for the portal, we need this so we can directly access its contents and render it elsewhere. This content will need to render content somewhere else in the component tree later on
This component is fairly simple and makes use of a small composition of an ng-template and ng-content as follows:
This also exposes the contents of the portal via the content Template Reference so that it can be accessed by components that will render this
Portal Renderer
The portal rendering component takes the content defined with the portal-content and renders it programatically somewhere - for our example we’re using the same component but this can render content elsewhere by passing around the TemplateRef we defined above
This component uses the <ng-template [cdkPortalOutlet]="contentOutput"></ng-template> to specify the template target. We will then use the @angular/cdk/portal module to construct the portal content:
Complete Code
A complete example using the two concepts demonstrated above can be seen below: