1

How can i load compontent from variable? Etc. I have an array

let arr: Array<string> = new Array('<comp1></comp1>','<comp2></comp2>');

in my html temple walk array, and get compontent, but only i see string as ''

<ng-container *ngFor="let item of arr">{{item}}</ngcontainer>

thanks for answers.

LSG
  • 61
  • 7

1 Answers1

2

You would need an array of types rather than of strings:

const types: ReadonlyArray<Type> = new Array(Comp1Class, Comp2Class);
<ng-container *ngFor="let componentTypeExpression of types">
  <ng-container *ngComponentOutlet="componentTypeExpression"></ng-container>
</ng-container>

Keep in mind they all must be in entryComponents of injector. For lazy modules you can add injector or even customize module factory: https://angular.io/api/common/NgComponentOutlet

waterplea
  • 3,462
  • 5
  • 31
  • 47
  • Hello, thanks for answer, but my array in a service in deep. I cant import every item in array. if i call simply in html is work. But i call *ngComponentOutlet="'ClassName'" is not working... – LSG Mar 20 '19 at 08:03
  • This is how you do it in Angular, you would have to refactor your service then, because even if you find some black magic that does what you ask, it would still be dark magic. Here's an Angular library that works with templates in React way by writing HTML in JS as strings, maybe it could help you: https://blog.angularindepth.com/introducing-to-ng-vdom-a-new-way-to-write-angular-application-60a3be805e59 – waterplea Mar 20 '19 at 11:04