I'm using ngTemplateOutlet with dynamic value.
<ng-container *ngFor="let part of objectKeys(config);">
<ng-container *ngIf="config[part]">
<ng-container [ngTemplateOutlet]="part"></ng-container>
</ng-container>
</ng-container>
<ng-template #one></ng-template>
<ng-template #two></ng-template>
config
is an objectconfig[part]
is a booleanpart
is the key of object and the value passed to ngTemplateOutlet.I always get the error :
ERROR TypeError: templateRef.createEmbeddedView is not a function
I've followed : https://stackoverflow.com/a/41241329/5627096
But maybe I can't do something like that.
Actually the config object contains boolean, like I said, and define the part of a form to display.
It's really big form and for better reading, I'm looking for a solution to split it.
UPDATE
config object looks like :
config = {
one: true,
two: false
}
So in my form only the <ng-template #one></ng-template>
is displayed. If I turn two to true, both are displayed.
I don't know if it's the best approach. I can use *ngIf but with this solution I have really unreadable big code.
ngTemplateOutlet is a structural directive. We use it to insert a template (created by ngTemplate ) in various sections of our DOM. For example, you can define a few templates to display an item and use them display at several places in the View and also swap that template as per the user's choice.
ng-template is an Angular element that is used for rendering HTML in a template. However, it is not rendered directly on DOM. If you include an ng-template tag to a template, the tag and the content inside it will be replaced by comment upon render.
To sum up, ng-content is used to display children in a template, ng-container is used as a non-rendered container to avoid having to add a span or a div, and ng-template allows you to group some content that is not rendered directly but can be used in other places of your template or you code.
One of the main uses for <ng-template> is to hold template content that will be used by Structural directives. Those directives can add and remove copies of the template content based on their own logic. When using the structural directive shorthand, Angular creates an <ng-template> element behind the scenes.
Add these to the components class
@ViewChild('one') one:TemplateRef<any>;
@ViewChild('two') two:TemplateRef<any>;
and get the template references using
<form no-validate (ngSubmit)="onSubmit(search)">
<ng-container *ngFor="let part of objectKeys(config);">
<ng-container *ngIf="config[part]">
<ng-container [ngTemplateOutlet]="this[part]"></ng-container>
</ng-container>
</ng-container>
</form>
StackBlitz example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With