Angular模板中的可重用片段

时间:2017-07-05 05:45:28

标签: angular angular2-template angular-ng-if

我们现在可以有else个代码段,引用<ng-template>

<div *ngIf="condition; else not">
  Condition satisfied.
  <ng-template #not>
    Condition not satisfied.
  </ng-template>
</div>

我希望能够在*ngIf的上下文之外引用代码段 - 类似

<div>DIV1 <ng-template-call template="shared"></ng-template-call></div>
<div>DIV2 <ng-template-call template="shared"></ng-template-call></div>

<ng-template #shared>This is a shared snippet.</ng-template>

在上面写上我所谓的ng-template-call的正确方法是什么?

是的,我知道我可以把它变成一个单独的组件,但它没有达到那个水平。我总是可以写:

<div *ngIf="false; else shared">

但这看起来很笨拙。

1 个答案:

答案 0 :(得分:9)

我认为Transclusions正是您正在寻找的

以下是一个示例:

<meta http-equiv="X-UA-Compatible" content="IE=5">

供参考:https://toddmotto.com/transclusion-in-angular-2-with-ng-content