Content Projection in Angular Element with Slot in Angular 7.0

In this article, we will learn how to project content in an Angular Element. If you are unfamiliar with the following,

  • Shadow Dom
  • ViewEncapsulation
  • Content Projection

I recommend you read the articles below before moving forward:


As of now, you know that we use ng-content to carry out content projection as shown in the next listing:

import { Component } from '@angular/core';
selector: 'app-greet',
template: `
export class GreetComponent {
title = 'Greet';

view raw


hosted with ❤ by GitHub

You can also project content as shown in the next listing:

Welcome to {{ title }}!
<h3>Hello Foo</h3>

view raw


hosted with ❤ by GitHub

The challenge with the above approach is, “If you use GreetComponent as your Angular Element,” you will not able to project content. To understand this better, let us start with converting GreetComponent to an Angular Element.  Here you can Learn Step by Step tp Create Angular Element.

After converting GreetComponent as Angular Element, AppModule should look like the listing below:

import { AppComponent } from './app.component';
import { GreetComponent } from './greet.component';
declarations: [
AppComponent, GreetComponent
imports: [
providers: [],
bootstrap: [GreetComponent],
entryComponents: [GreetComponent]
export class AppModule {
constructor(private injector: Injector) {
const customElement = createCustomElement(GreetComponent, { injector });
customElements.define('app-root', customElement);
ngDoBootstrap() {

view raw


hosted with ❤ by GitHub

Now you can use GreetComponent on index.html as shown in the listing below:

<!– <app-root></app-root> –>
<h2>hey Foo</h2>

view raw


hosted with ❤ by GitHub

Upon running the application, you will find that the <h2> element has not been projected to the Angular Element GreetComponent.

Read full article on the Infragistics blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at