{"id":743,"date":"2018-02-03T03:05:00","date_gmt":"2018-02-03T03:05:00","guid":{"rendered":"https:\/\/staging.infragistics.com\/blogs\/?p=743"},"modified":"2025-02-19T13:41:59","modified_gmt":"2025-02-19T13:41:59","slug":"dynamically-create-a-component-in-angular","status":"publish","type":"post","link":"https:\/\/www.infragistics.com\/blogs\/dynamically-create-a-component-in-angular","title":{"rendered":"How To Dynamically Create a Component in Angular"},"content":{"rendered":"\n<p>Let us assume that, we have a component as listed below, which we will load dynamically.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import\u00a0{\u00a0Component,\u00a0Input\u00a0}\u00a0from\u00a0'@angular\/core';\n \n@Component({\n\u00a0\u00a0\u00a0\u00a0selector:\u00a0'app-message',\n\u00a0\u00a0\u00a0\u00a0template:\u00a0`&lt;h2>{{message}}&lt;\/h2>\n`\n})\nexport\u00a0class\u00a0MessageComponent\u00a0{\n\u00a0\u00a0\u00a0\u00a0@Input()\u00a0message:\u00a0string;\n}<\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Template of AppComponent is as below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;div\u00a0style=\"text-align:center\">\n\u00a0\u00a0\u00a0\u00a0\u00a0&lt;h1>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Welcome\u00a0to\u00a0{{\u00a0title\u00a0}}!\n\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/h1>\n\u00a0\u00a0\u00a0\u00a0\u00a0&lt;template\u00a0#messagecontainer>\n\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/template>\n\u00a0&lt;\/div><\/pre>\n\n\n\n<p>As you see that, we have an entry point template or a container template in which we will load MessageComponent dynamically.<\/p>\n\n\n\n<p>In the AppComponent, we need to import following:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>ViewChild, ViewContainerRef, and ComponentFactoryResolver from @angular\/core<\/li>\n\n\n\n<li>ComponentRef and ComponentFactory from @angular\/core<\/li>\n\n\n\n<li>MessageComponent from message.component<\/li>\n<\/ol>\n\n\n\n<p>After importing required things, AppComponnet will look like following listing:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import\u00a0{\n\u00a0\u00a0\u00a0\u00a0Component,\n\u00a0\u00a0\u00a0\u00a0ViewChild,\n\u00a0\u00a0\u00a0\u00a0ViewContainerRef,\n\u00a0\u00a0\u00a0\u00a0ComponentFactoryResolver,\n\u00a0\u00a0\u00a0\u00a0ComponentRef,\n\u00a0\u00a0\u00a0\u00a0ComponentFactory\n}\u00a0from\u00a0'@angular\/core';\nimport\u00a0{\u00a0MessageComponent\u00a0}\u00a0from\u00a0'.\/message.component';\n \n@Component({\n\u00a0\u00a0\u00a0\u00a0selector:\u00a0'app-root',\n\u00a0\u00a0\u00a0\u00a0templateUrl:\u00a0'.\/app.component.html'\n})\nexport\u00a0class\u00a0AppComponent\u00a0{\n\u00a0\u00a0\u00a0\u00a0title\u00a0=\u00a0'app';\n}<\/pre>\n\n\n\n<p>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 <strong>ViewConatinerRef<\/strong>.<\/p>\n\n\n\n<p><strong>ViewContainerRef<\/strong> represents container where one or more view can be attached. This can contain two types of views.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Host Views<\/li>\n\n\n\n<li>Embedded Views<\/li>\n<\/ol>\n\n\n\n<p>&nbsp;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.<\/p>\n\n\n\n<p>Let us create a variable called entry which will refer template element. In addition, we have injected <strong>ComponentFactoryResolver<\/strong> services to component class, which will be needed to dynamically load the component.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export\u00a0class\u00a0AppComponent\u00a0{\n\u00a0\u00a0\u00a0\u00a0title\u00a0=\u00a0'app';\n\u00a0\u00a0\u00a0\u00a0@ViewChild('messagecontainer',\u00a0{\u00a0read:\u00a0ViewContainerRef\u00a0})\u00a0entry:\u00a0ViewContainerRef;\n\u00a0\u00a0\u00a0\u00a0constructor(private\u00a0resolver:\u00a0ComponentFactoryResolver)\u00a0{\u00a0}\n}<\/pre>\n\n\n\n<p>Keep in mind that entry variable which is reference of template element has API to create components, destroy components etc.<\/p>\n\n\n\n<p>Now to create component, let us create a function. Inside the function, we need to perform following tasks,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clear the container<\/li>\n\n\n\n<li>Create a factory for MessageComponent<\/li>\n\n\n\n<li>Create component using the factory<\/li>\n\n\n\n<li>Pass value for @Input properties using component reference instance method<\/li>\n<\/ul>\n\n\n\n<p>Putting everything, together createComponent function will look like listing below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">createComponent(message)\u00a0{\n\u00a0\u00a0\u00a0\u00a0this.entry.clear();\n\u00a0\u00a0\u00a0\u00a0const\u00a0factory\u00a0=\u00a0this.resolver.resolveComponentFactory(MessageComponent);\n\u00a0\u00a0\u00a0\u00a0const\u00a0componentRef\u00a0=\u00a0this.entry.createComponent(factory);\n\u00a0\u00a0\u00a0\u00a0componentRef.instance.message\u00a0=\u00a0message;\n}<\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;div\u00a0style=\"text-align:center\">\n\u00a0\u00a0\u00a0\u00a0&lt;h1>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Welcome\u00a0to\u00a0{{\u00a0title\u00a0}}!\n\u00a0\u00a0\u00a0\u00a0&lt;\/h1>\n\u00a0\u00a0\u00a0\u00a0&lt;button\u00a0(click)=\"createComponent('Welcome\u00a0Foo\u00a0!\u00a0')\">Welcome&lt;\/button>\n\u00a0\u00a0\u00a0\u00a0&lt;button\u00a0(click)=\"createComponent('Foo\u00a0Again\u00a0?')\">Not\u00a0Welcome&lt;\/button>\n\u00a0\u00a0\u00a0\u00a0&lt;br\u00a0\/>\n\u00a0\u00a0\u00a0\u00a0&lt;template\u00a0#messagecontainer>\n\u00a0\u00a0\u00a0\u00a0&lt;\/template>\n&lt;\/div><\/pre>\n\n\n\n<p>In output, you can see that component is getting loaded dynamically on click of the button.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/00\/00\/00\/09\/43\/2543.abc.png\" alt=\" welcome app\" title=\" welcome app\"\/><\/figure>\n\n\n\n<p>As you click on the buttons component will be reloaded with different message. &nbsp;You can destroy a component using destroy method on the componentRef.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">destroyComponent()\u00a0{\n\u00a0\u00a0\u00a0\u00a0this.componentRef.destroy();\n}<\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Putting everything together, <strong>AppComponent<\/strong> will look like listing below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import\u00a0{\n\u00a0\u00a0\u00a0\u00a0Component,\n\u00a0\u00a0\u00a0\u00a0ViewChild,\n\u00a0\u00a0\u00a0\u00a0ViewContainerRef,\n\u00a0\u00a0\u00a0\u00a0ComponentFactoryResolver,\n\u00a0\u00a0\u00a0\u00a0ComponentRef,\n\u00a0\u00a0\u00a0\u00a0ComponentFactory\n}\u00a0from\u00a0'@angular\/core';\nimport\u00a0{\u00a0MessageComponent\u00a0}\u00a0from\u00a0'.\/message.component';\n \n@Component({\n\u00a0\u00a0\u00a0\u00a0selector:\u00a0'app-root',\n\u00a0\u00a0\u00a0\u00a0templateUrl:\u00a0'.\/app.component.html'\n})\nexport\u00a0class\u00a0AppComponent\u00a0{\n\u00a0\u00a0\u00a0\u00a0title\u00a0=\u00a0'app';\n\u00a0\u00a0\u00a0\u00a0componentRef:\u00a0any;\n \n\u00a0\u00a0\u00a0\u00a0@ViewChild('messagecontainer',\u00a0{\u00a0read:\u00a0ViewContainerRef\u00a0})\u00a0entry:\u00a0ViewContainerRef;\n\u00a0\u00a0\u00a0\u00a0constructor(private\u00a0resolver:\u00a0ComponentFactoryResolver)\u00a0{\u00a0}\n \n\u00a0\u00a0\u00a0\u00a0createComponent(message)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.entry.clear();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0const\u00a0factory\u00a0=\u00a0this.resolver.resolveComponentFactory(MessageComponent);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.componentRef\u00a0=\u00a0this.entry.createComponent(factory);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.componentRef.instance.message\u00a0=\u00a0message;\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0destroyComponent()\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.componentRef.destroy();\n\u00a0\u00a0\u00a0\u00a0}\n}<\/pre>\n\n\n\n<p>At this point on running application, you will get an error because we have not set the <strong>entryComponents<\/strong> in the AppModule. We can set that as shown in the listing below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import\u00a0{\u00a0AppComponent\u00a0}\u00a0from\u00a0'.\/app.component';\nimport\u00a0{\u00a0MessageComponent\u00a0}\u00a0from\u00a0'.\/message.component';\n \n@NgModule({\n\u00a0\u00a0\u00a0\u00a0declarations:\u00a0[\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0AppComponent,\u00a0MessageComponent\n\u00a0\u00a0\u00a0\u00a0],\n\u00a0\u00a0\u00a0\u00a0imports:\u00a0[\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0BrowserModule\n\u00a0\u00a0\u00a0\u00a0],\n\u00a0\u00a0\u00a0\u00a0providers:\u00a0[],\n\u00a0\u00a0\u00a0\u00a0bootstrap:\u00a0[AppComponent],\n\u00a0\u00a0\u00a0\u00a0entryComponents:\u00a0[MessageComponent]\n})\nexport\u00a0class\u00a0AppModule\u00a0{\u00a0}<\/pre>\n\n\n\n<p>This is all you need to do to load a component dynamically in Angular.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><span style=\"color: #333333; font-family: 'Arial',sans-serif;\">Like this post?<\/span><\/strong><\/h4>\n\n\n\n<p><span style=\"color: #666666; font-family: 'Arial',sans-serif;\">And there you have it! If you like this post, please like it and share it. In addition, if you haven\u2019t checked out\u00a0<\/span><a href=\"\/products\/ignite-ui-angular\"><span style=\"color: #03a9f4; font-family: 'Arial',sans-serif;\">Infragistics Ignite UI for Angular<\/span><\/a><span style=\"color: #666666; font-family: 'Arial',sans-serif;\">, be sure to do so! They\u2019ve got 50+ material-based Angular components to help you code speedy web apps faster.<\/span><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 modal etc.<\/p>\n","protected":false},"author":65,"featured_media":1424,"comment_status":"publish","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-743","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular"],"_links":{"self":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/comments?post=743"}],"version-history":[{"count":3,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/743\/revisions"}],"predecessor-version":[{"id":1960,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/743\/revisions\/1960"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media\/1424"}],"wp:attachment":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media?parent=743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/categories?post=743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/tags?post=743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}