Skip to content
Advertisement

How to use the same template in ngIf

I have many conditions to show the same template. For example:

JavaScript

Is it possible to take this html elements:

JavaScript

and put somewhere and then just call this html elements by name/reference in *ngIf? For example:

JavaScript

Advertisement

Answer

Actually ngIf has a ‘cool’ attribute, then, that you could make use of:

JavaScript

As <template> has been deprecated, use <ng-template> instead or <ng-container>. You can remove the second ngIf in the template as the first is sufficient.

Stackblitz

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement