Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 how to pass data into dynamic component with NgComponentOutlet

Tags:

angular

I fount this similar question. angular 4+ assign @Input for ngComponentOutlet dynamically created component

But it has been about a month. Has anything changed?

Basically, I followed this guide and created a dynamic component: https://angular.io/docs/ts/latest/cookbook/dynamic-component-loader.html

With this approach, I can assign a value to the dynamic component: (<AdComponent>componentRef.instance).data = adItem.data;

Is it still true I can't assign a value to the dynamic component with NgComponentOutlet out of the box? (https://angular.io/docs/ts/latest/api/common/index/NgComponentOutlet-directive.html)

like image 564
techguy2000 Avatar asked Oct 27 '25 16:10

techguy2000


1 Answers

You can pass in a custom injector like this - https://github.com/angular/angular/issues/16373#issuecomment-306544456

This was kind of a hack so we ended up using this library-

https://www.npmjs.com/package/ng-dynamic-component

Worked like a charm!

like image 84
user6083264 Avatar answered Oct 30 '25 09:10

user6083264



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!