Skip to content

Angular Dynamic Component

DodaTech 2 min read

In this tutorial, you'll learn about Angular Dynamic Component Error Fix. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Error: Component MyComponent is not part of any NgModule or the module has not been imported into your module.

Dynamic components must be declared in an NgModule or added to entryComponents.

Wrong

import { Component } from '@angular/core'

@Component({
  template: `<p>Dynamic component</p>`,
})
export class MyComponent {}
// Not declared in any module

Output: Angular cannot instantiate the component dynamically.

In Angular 9+ with Ivy, add the component to declarations:

@NgModule({
  declarations: [MyComponent],
})
export class FeatureModule {}

Create the component dynamically:

@Component({
  template: `<ng-template #container></ng-template>`,
})
export class HostComponent {
  @ViewChild('container', { read: ViewContainerRef }) container: ViewContainerRef

  constructor(private componentFactoryResolver: ComponentFactoryResolver) {}

  ngAfterViewInit() {
    const factory = this.componentFactoryResolver.resolveComponentFactory(MyComponent)
    this.container.createComponent(Factory)
  }
}

Expected output: MyComponent renders inside the ng-template container.

Prevention

  • Declare dynamic components in an NgModule
  • In Angular 13+, use ViewContainerRef.createComponent() with the component class directly
  • Add components to entryComponents in Angular versions before Ivy

Common Mistakes with dynamic component

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world Angular code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### How does dynamic component creation work in Angular?

Angular uses ComponentFactoryResolver to create a Factory for the component, then ViewContainerRef.createComponent() to instantiate it in the view.

What is the difference between static and dynamic components?

Static components are declared in templates. Dynamic components are created programmatically at runtime using the component Factory or ViewContainerRef.

Can I pass inputs to dynamic components?

Yes. Access the component instance: const ref = this.container.createComponent(Factory); ref.instance.inputProperty = value;.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro