Angular/typescript load component from variable

时间:2019-03-19 15:11:44

标签: angular typescript

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.

1 个答案:

答案 0 :(得分:1)

您将需要一个类型数组而不是字符串:

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

请记住,它们都必须位于注射器的entryComponents中。对于惰性模块,您可以添加注入器,甚至可以定制模块工厂: https://angular.io/api/common/NgComponentOutlet