I really like the answer provided for Dynamic template based on value rather than variable with ngTemplateOutlet. However I'm not able to get it to work. Simplified example:
export class A {
}
export class MyComponent
implements OnInit {
public controls$ = Observable<any[]>([]);
ngOnInit() {
this.controls$.next([new A()]);
}
public getTypeName(control: any) {
if (control instanceof A) {
return "AControl";
}
return "";
}
}
Template:
<div *ngFor="let control of control$ | async">
{{ getControlType(control) }}
</div>
Yields:
AControl
So far so good. When I add a template, I get an exception:
<div *ngFor="let control of control$ | async">
{{ getControlType(control) }}
<ng-container
[ngTemplateOutlet]="getControlType(control)"
[ngTemplateOutletContext]="{ control: control }">
</ng-container>
</div>
<ng-template
#AControl
let-item="control">A Control</ng-template>
throws:
templateRef.createEmbeddedView is not a function
I'm not sure what I need to change in order for the template #AControl
to render in the container.