Scipion
Scipion

Reputation: 11888

Angular2 ng-template

I am using the version 2.4.9 of Angular. I would like to declare an html element that I can use in several places :

<div #template>
 foo
</div>
<header>
 {{ template }}
</header>
<body>
 {{ template }}
</body>

It obviously doesn't work, and the solution would be to create a component. I have seen in angular4 that something calls ng-template does exist. Is there anything similar in angular 2.4.X ?

Upvotes: 4

Views: 7938

Answers (1)

yurzui
yurzui

Reputation: 214007

There is ngTemplateOutlet directive that can help you:

<template #myTemplate>
   foo
</template>
<header>
  <ng-container *ngTemplateOutlet="myTemplate"></ng-container>
</header>
<div>
  <ng-container *ngTemplateOutlet="myTemplate"></ng-container>
</div>

Upvotes: 12

Related Questions