We are going to have a modal component:

<au-modal >

</au-modal>

And we can pass default modal body by content projection:

<au-modal >
<modal-body></modaö-body>
</au-modal>

So 'modal-body' will be shown by default.

Now we want to modal body can be configurable. We can choose to pass in a new template thought @Input, if template was passed in then use template instead of 'modal-body':

<ng-template #newModalBody>
<!-- template goes here-->
</ng-template> <au-modal [body]="newModalBody">
<au-modal-body></au-modal-body>
</au-modal>

To do that, we defined a 'ng-template' and mark it as 'newModalBody' templateRef. It contians ours new template. And we can pass the template thought @Input.

So in the au-modal, it defines:

import {Component, Input, OnInit, TemplateRef} from '@angular/core';

@Component({
selector: 'au-modal',
templateUrl: './au-modal.component.html',
styleUrls: ['./au-modal.component.scss']
})
export class AuModalComponent implements OnInit { @Input() body: TemplateRef<any>;
constructor() { } ngOnInit() {
} }

In the component html, we need to check whether we pass in the template or not, if new template is present then we use it, otherwise, we fallback to default content projection:

<div class="modal-overlay">

  <div class="modal-body">

    <ng-container *ngIf="body else projectionBody">
<ng-container *ngTemplateOutlet="body"></ng-container>
</ng-container> <ng-template #projectionBody>
<ng-content></ng-content>
</ng-template> </div> </div>

The reason here we use two ng-container is because, for one ng-container can only have one structure directive (*ngIf or *ngTeplateOutlet).

最新文章

  1. Socket通信原理探讨(C++为例)
  2. MEF Parts Sample
  3. ycm添加自定义补全路径
  4. Atitit attilax总结的对于attilax重要的jsr规范,以及需要增加的jsr规范
  5. 2016全国研究生数学建模A题多无人机协同任务规划——基于分布式协同多旅行商MTSP遗传算法
  6. MongoDB学习笔记一:入门
  7. UWP开发入门(十三)——用Diagnostic Tool检查内存泄漏
  8. Web打印控件
  9. Java SE 第二十一讲----抽象类
  10. 数据结构——Java实现二叉树
  11. 用GPUImage开启相机并且开启滤镜效果
  12. 14.4.2 Change Buffer 延迟写
  13. android UI进阶之用【转】
  14. [置顶] android系统功能调用大全
  15. Minigui开发之遥控控制逻辑算法
  16. (HTTPS)web 项目如何实现https
  17. 201521123113《Java程序设计》第14周学习总结
  18. WindowsAll下安装与破解IntelliJ IDEA2017
  19. java大数相加
  20. 调试lldb命令常用

热门文章

  1. 37.cgi网页交互
  2. 用AOP改善javascript代码
  3. 欧拉回路&amp;欧拉路径学习笔记
  4. Java中join和yield的作用
  5. [React Native] Animate the Scale of a React Native Button using Animated.spring
  6. Android实战简易教程-第二十五枪(基于Baas的数据表查询下拉刷新和上拉载入实现!)
  7. LeetCode_ZigZag Conversion
  8. Spring中事务的XML方式[声明方式]
  9. golang标准包中文手册
  10. 洛谷 P1303 A*B Problem