How to dynamically inject components into the DOM using Component

0 votes
Can i know How to dynamically inject components into the DOM using @Component?
4 days ago in Node-js by Nidhi
• 14,600 points
39 views

1 answer to this question.

0 votes

You need to use ComponentFactoryResolver and ViewContainerRef. Here's the precise approach:

1. Create a Dynamic Component

First, ensure your component is declared as entryComponent (for Angular versions < 9) or properly included in your module:

@Component({

  selector: 'app-dynamic',

  template: `<h2>Dynamic Component</h2>`

})

export class DynamicComponent {}

2. Implement the Dynamic Injection

In your parent component:

@Component({

  selector: 'app-parent',

  template: `

    <ng-template appComponentHost></ng-template>

    <button (click)="loadComponent()">Load Component</button>

  `

})

export class ParentComponent implements OnInit {

  @ViewChild(ComponentHostDirective, {static: true}) componentHost: ComponentHostDirective;

  constructor(private componentFactoryResolver: ComponentFactoryResolver) {}

  loadComponent() {

    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(DynamicComponent);

    const viewContainerRef = this.componentHost.viewContainerRef;

    viewContainerRef.clear();

    viewContainerRef.createComponent(componentFactory);

  }

}

3. Update Your Module

@NgModule({

  declarations: [

    ParentComponent,

    DynamicComponent,

    ComponentHostDirective

  ],

  entryComponents: [DynamicComponent] // Required for Angular < 9

})

export class AppModule { }

answered 3 days ago by anonymous

Related Questions In Node-js

0 votes
1 answer

How to reload or re-render the entire page using AngularJS?

Hello @kartik< For the record, to force angular ...READ MORE

answered Jul 15, 2020 in Node-js by Niroj
• 82,840 points
5,409 views
0 votes
1 answer

How to set the content-type of request header when using Fetch APi?

Hello @kartik, You need to create a fetch ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,840 points
8,353 views
0 votes
1 answer

How do I get the path to the current script with Node.js?

Hello @kartik, you can do this: fs.readFile(path.resolve(__dirname, 'settings.json'), 'UTF-8', ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
3,150 views
0 votes
1 answer

How to create a directory if it doesn't exist using Node.js?

Hello @kartik, Try: var fs = require('fs'); var dir = ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,840 points
6,450 views
0 votes
1 answer

How to handle the swiperight event to trigger custom actions in jQuery Mobile?

To handle the swiperight event and trigger ...READ MORE

answered 3 days ago in Node-js by anonymous
30 views
0 votes
1 answer
0 votes
1 answer

How to implement a directive that auto-saves form data periodically?

To create a directive that automatically saves ...READ MORE

answered 3 days ago in Node-js by anonymous
28 views
0 votes
1 answer

How to develop a directive that restricts user input based on custom conditions?

To create an Angular directive that restricts ...READ MORE

answered 3 days ago in Node-js by anonymous
31 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
4,171 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP