繁体   English   中英

ng-template内部的ng-content仅出现在一个ng容器中

[英]ng-content inside ng-template appears only in one ng-container

我正在创建一个翻转面板。 面板分为三个部分。

  • 面板工具条
  • 面板前
  • 面板背

工具栏上有一个固定部件,即翻转按钮。

在我的flip-panel.component中,我有一个用于工具栏的ng模板,因为工具栏应显示在两侧并且应该相同。 但是我还在此模板中使用ng-content来获取用户定义的工具栏项目。

我的flip-panel.component:

<div class="flip-panel">

  <div class="front-container">
    <ng-container [ngTemplateOutlet]="headerTemplate"></ng-container>
    <ng-content select="panel-front"></ng-content>
  </div>

  <div class="back-container">
    <ng-container [ngTemplateOutlet]="headerTemplate"></ng-container>
    <ng-content select="panel-back"></ng-content>
  </div>

</div>

<!-- template used in both container front and back -->
<ng-template #headerTemplate>
  <div class="flip-panel-header">
    <span>
      <ng-content select="panel-tool-bar"></ng-content>
    </span>
    <span class="flip-button" aria-hidden="true" (click)="toggle()">[Flip]</span>
  </div>
</ng-template>

我遇到的问题是,工具栏中的用户定义项目(从面板工具栏中选择的内容)仅显示在背面的内容上。

这是一个完整的示例: https : //stackblitz.com/edit/angular-fsnww2?file=src%2Fapp%2Fapp.component.html

您只能使用一次内容投影,并且尝试通过ng-content加载标头。 例如,确保使用* ngIf,内容仅投影一次。

工作示例代码:

<div class="flip-panel">
  <div class="front-container">
    <ng-container *ngIf="flipped" [ngTemplateOutlet]="headerTemplate"></ng-container>
    <ng-content select="panel-front"></ng-content>
  </div>

  <div class="back-container">
    <ng-container *ngIf="!flipped" [ngTemplateOutlet]="headerTemplate"></ng-container>
    <ng-content select="panel-back"></ng-content>
  </div>
</div>

暂无
暂无

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

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