简体   繁体   English

Angular2 ng-template

[英]Angular2 ng-template

I am using the version 2.4.9 of Angular. 我正在使用Angular的2.4.9版本。 I would like to declare an html element that I can use in several places : 我想声明一个我可以在几个地方使用的html元素:

<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. 我在angular4中看到过一些叫做ng-template东西确实存在。 Is there anything similar in angular 2.4.X ? 角度2.4.X有什么相似之处吗?

There is ngTemplateOutlet directive that can help you: ngTemplateOutlet指令可以帮助您:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM