Angular Dynamic Component Loader Tutorial with Examples

Angular Dynamic Component Loader Tutorial with Examples

In the tutorial, grokonez.com 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

Overview

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.

angular-dynamic-loading-component-tutorial-with-examples-overview

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:

SportNewsComponent:

BusinessNewsComponent:

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:

Sourcecode

– Project structure

angular-dynamic-loading-component-tutorial-with-examples-project-structure

– Source Code:

Angular-Dynamic-Loading-Components

By grokonez | June 10, 2019.



Related Posts


Got Something To Say:

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

*