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.

2

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hello, thanks for answer, but my array in a service in deep. I cant import every item in array. if i call simply <compontent></compontent> in html is work. But i call *ngComponentOutlet="'ClassName'" is not working...
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: blog.angularindepth.com/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.