在组件选择器中添加自定义模板Html

时间:2018-01-18 10:52:14

标签: angular

  

有没有办法在组件选择器中添加自定义模板html?

我有一个带有选择器#include "string.h" // points to my library include #include <stdio.h> // points to C stdlib include 的组件AppToolbarComponent,它有一个空白模板。我想在两个不同的组件上使用此组件,但在使用组件时定义了模板。例如:

<app-toolbar>

page-one.component.html

...以及 <app-toolbar> <button (click)="onClickOne()">Button in Component One</button> </app-toolbar>

page-two.component.html

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

  

根据 @pixelbits 的评论,这是一个答案:

<app-toolbar>中,在组件模板中添加<ng-content>。这是一个例子:

<div>Some Content</div>
<ng-content></ng-content>

现在,<app-toolbar>选择器中添加的任何内容都将在<ng-content>内呈现。例如:

<app-toolbar>
    <!-- The following content will render inside <ng-content> 
         in the app-toolbar component -->
    <button (click)="onClickOne()">Button in Component One</button>
</app-toolbar>

以下是StackBlitz Demo的链接。