adm*_*r86 1 ng-template angular ng-container ng-content
我正在创建一个翻转面板。该面板由三部分组成。
工具栏有一个固定部分,即翻转按钮。
在我的 Flip-panel.component 中,我有一个工具栏的 ng-template,因为工具栏应该显示在两侧并且应该是相同的。但我还在该模板中使用 ng-content 来获取工具栏的用户定义项目。
我的翻转面板组件:
<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>
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,工具栏中用户定义的项目(从面板工具栏中选择的内容)仅显示在背面内容上。
这是一个完整的示例: 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5583 次 |
| 最近记录: |