<ng-template #reuse>
<div>Hello World</div>
</ng-template>
<ng-container [ngTemplateOutlet]="reuse"></ng-container>
<ng-container [ngTemplateOutlet]="reuse">
<p>Good Morning</p>
</ng-container>
I would like to reuse the reuse template reference multiple times but with small modifications each time.
In the above example, in the 2nd <ng-container>
I would also like to add an additional <p>
tag after the <div>
tag (in the <ng-template>
). Is it possible to achieve that without creating another template?
What is the end goal here? When or how happens this "each time"?
ng-template
is never generated in html code, it generates it's contents as html at run time. So adding new html lines to a template is not possible. It is a dynamic abstraction of Angular syntax.However you could use template variables to pre-configure the template to add new lines when themplate bound variables change.
For example, you could add more template variables by adding
let
tong-template
. https://angular.io/guide/template-reference-variables#template-input-variableExample: https://stackblitz.com/edit/hello-angular-6-ng-template-and-ng-container-exam-w2zn1j?file=src%2Fapp%2Fapp.component.html