Angular Dynamic Component Loader Tutorial with Examples

Angular Dynamic Component Loader Tutorial with Examples

In the tutorial, shows how to dynamically load an Angular components by examples. What we introduce:

  • Create Anchor Directive
  • Dynamically Loading and Resolving Components

Related posts:
Angular 6 Component – How to create & integrate New Angular 6 Component
Angular 6 Routing/Navigation – with Angular Router Service


An Agnular application may need to load new components at runtime, so component’s templates are not always fixed.
The following examples shows how to build an dynamic news banner.


New news components are added frequently so it is impractical to use a template with a static component structure.

Angular Anchor Directive

We need define an anchor point to tell Angular where to insert components for adding a new components:
Now we build an NewsDirective to mark valid insertion points in the template:

NewsDirective injects ViewContainerRef to access to the view container of the element that will host the dynamically added component.

news-host is used to apply the directive to the element.

Angular Dynamic Loading Components

We implement an NewsBannerComponent in news-banner.component.ts file.

The element is where you apply the directive to dynamically load components.
The element doesn’t render any additional output.

Angular Resolving Components

NewsBannerComponent takes an array of NewsItem objects as input that comes NewsService.
NewsItem object contains component object to load and any data to bind to the component.

NewsService returns the actual news content for banner:

NewsBannerComponent loop through the array of NewsItems and uses loadComponent() to load a new component every 3 seconds.

NewsBannerComponent uses ComponentFactoryResolver to resolve a ComponentFactory for each specific component.
createComponent() method returns a reference to the loaded component. Use that reference to interact with the component by assigning to its properties or calling its methods.

Note: to ensure that the compiler generates a ComponentFactory for dynamically loaded components, we need add dynamically loaded components to the NgModule’s entryComponents array:

NewsComponent Interface

To standardize the API for passing data to the components, we create an interface component: NewsComponent.

Then using it in all sub-news components:



Bootstrap App Component

We edit a bootstrap AppComponent that shows app-news-banner component. And it also injects NewsService to provide NewsItem data for NewsBannerComponent:


– Project structure


– Source Code:


By grokonez | June 10, 2019.

Related Posts

26 thoughts on “Angular Dynamic Component Loader Tutorial with Examples”

  1. Ahaa, its nice dialogue regarding this post at this place at this weblog, I have read all that, so now me also commenting here. privatleasing bil bra eller dГҐligt

  2. Белорусский трикотаж Свитмода|Молодежная женская одежда Свитмода|Одежда женская больших размеров Свитмода Бай|

  3. Белорусский трикотаж Свитмода|Молодежная женская одежда Свитмода|Одежда женская больших размеров Свитмода Бай|

  4. I like the valuable info you provide in your articles. I’ll bookmark your blog and check again here regularly. I am quite certain I’ll learn plenty of new stuff right here! Good luck for the next! Kissee Kalvin Marja

  5. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I have a dynamic component loader, and I need to pass in data through a service. I can get the data to display if I fire the function on

Got Something To Say:

Your email address will not be published. Required fields are marked *