In this article, we will learn to create a component dynamically. You may need to load a component dynamically in various scenarios such as want to show a popup model etc.
Let us assume that, we have a component as listed below, which we will load dynamically.
To load MessageComponent dynamically you need a container. Let us say that we want to load MessageComponent inside AppComponent. We need a container element in the AppComponent.
Template of AppComponent is as below:
As you see that, we have an entry point template or a container template in which we will load MessageComponent dynamically.
In the AppComponent, we need to import following:
- ViewChild, ViewContainerRef, and ComponentFactoryResolver from @angular/core
- ComponentRef and ComponentFactory from @angular/core
- MessageComponent from message.component
After importing required things, AppComponnet will look like following listing:
We can access template as the ViewChild inside the Component class. Template is a container in which, we want to load the component dynamically. Therefore, we have to access temple as ViewConatinerRef.
ViewContainerRef represents container where one or more view can be attached. This can contain two types of views.
- Host Views
- Embedded Views
Host Views are created by instantiating a component using createComponent and Embedded Views are created by instantiating an Embedded Template using createEmbeddedView. We will use Host Views to dynamically load MessageComponent.
Let us create a variable called entry which will refer template element. In addition, we have injected ComponentFactoryResolver services to component class, which will be needed to dynamically load the component.
Keep in mind that entry variable which is reference of template element has API to create components, destroy components etc.
Now to create component, let us create a function. Inside the function, we need to perform following tasks,
- Clear the container
- Create a factory for MessageComponent
- Create component using the factory
- Pass value for @Input properties using component reference instance method
Putting everything, together createComponent function will look like listing below:
We can call createComponent function on click event of the button. Let us put two buttons in the template and call createComponent function on click of the buttons.
In output, you can see that component is getting loaded dynamically on click of the button.
As you click on the buttons component will be reloaded with different message. You can destroy a component using destroy method on the componentRef.
Either you can destroy dynamically loaded component by manually calling the function or put it inside ngOnDestroy() life cycle hook of the component, such that when host component is destroyed automatically dynamically loaded component will also destroy.
Putting everything together, AppComponent will look like listing below:
At this point on running application, you will get an error because we have not set the entryComponents in the AppModule. We can set that as shown in the listing below:
This is all you need to do to load a component dynamically in Angular.
Like this post?
And there you have it! If you like this post, please like it and share it. In addition, if you haven’t checked out Infragistics Ignite UI for Angular, be sure to do so! They’ve got 30+ material based Angular components to help you code speedy web apps faster.
This post was originally published on the Infragistics blog.