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).

[Angular] Configurable Angular Components - Content Projection and Input Templates的更多相关文章

  1. [Angular 2] Share Template Content In Another Template With Content Projection <ng-content>

    Angular 1 provided a mechanism to place content from your template inside of another template called ...

  2. [Angular] Learn Angular Multi-Slot Content Projection

    Now for au-modal component, we pass in tow component though contenct projection: <au-modal class= ...

  3. [Angular] Content Projection with ng-content

    For example there is tow form compoennts on the page, and what we want to do is reusing the form com ...

  4. Angular 2 to Angular 4 with Angular Material UI Components

    Download Source - 955.2 KB Content Part 1: Angular2 Setup in Visual Studio 2017, Basic CRUD applicat ...

  5. [Angular 2] Angular 2 Smart Components vs Presentation Components

    Both Smart Components and Presentation Components receive data from Services in entirely different w ...

  6. angular 2 angular quick start Could not find HammerJS

    Angular2 的material中 引用了 hammerjs,遇到Could not find HammerJS错误,正确的步骤如下: 需要在如下位置增加 对material 和 hammerjs ...

  7. ASP.NET Core 2.1 Web API + Identity Server 4 + Angular 6 + Angular Material 实战小项目视频

    视频简介 ASP.NET Core Web API + Angular 6的教学视频 我是后端开发人员, 前端的Angular部分讲的比较差一些, 可以直接看代码!!!! 这是一个小项目的实战视频, ...

  8. [Angular] Use Angular components in AngularJS applications with Angular Elements

    When migrating AngularJS (v1.x) applications to Angular you have different options. Using Angular El ...

  9. angular自定义指令解决IE89不支持input的placeholder属性

    下面代码实测通过,直接copy到本地运行即可. <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

随机推荐

  1. poj3244(公式题)

    Difference between Triplets Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2476   Acce ...

  2. cocos2dx-js学习笔记(一)环境搭建

    本人眼下的学习方向是cocos2dx+js的开发方式,开发调试使用webstrom和火狐浏览器,调试完毕的项目使用cocos2dx+jsb的方式编译到PC或android设备执行.主要时间用在学习,所 ...

  3. Objective-C基础笔记(1)基本概念和第一个程序

    一.基本概念 Objective-C(简称OC)是iOS开发的核心语言,苹果公司在维护,在开发过程中也会配合着使用C语言.C++,OC主要负责UI界面,C语言.C++可用于图形处理.C语言是面向过程的 ...

  4. Switchover and Failover说明

    SWITCHOVER Switchover是有计划的将primary切换为standby,standby切换为primary.在主库结束生产后,备库应用完所有主库archivelog或者redo lo ...

  5. 通过no-gui模式运行jmeter脚本与生成报告

    说明:使用NO-GUI 模式,即命令行模式运行 JMeter 测试脚本能够大大缩减所需要的系统资源. 步骤:在GUI(图形化界面)模式调整好脚本,通过FTP工具将需要测试的.jmx文件传输到linux ...

  6. 2.1 使用eclipse4.4 搭建 maven简单结构项目。

    1.前言 1.本博客面向0基础开发人员. 2.本博客为系列博客.<1.X>系列为服务器数据库相关技术,前几章为简单搭建linux+tomcat+mysql+nginx+redis.< ...

  7. Xcode6:No architectures to compile for(ONLY_ACTIVE_ARCH=YES...)

    1.问题描写叙述 Xcode6真机測试旧project,不能执行,报错例如以下: 2.解决方式 问题非常明显,当前的执行模式设置了ONLY_ACTIVE_ARCH为Yes,当时的project的Val ...

  8. ImageButton-设置background跟src

    xml中添加ImageButton的background跟src <ImageButton android:id="@+id/tv3" android:layout_widt ...

  9. Android中实现整个视图切换的左右滑动效果

    Android中提供了一个Gallary,可以实现图片或者文本的左右滑动效果. 如何让整个视图都能实现左右滑动,达到类似于Gallary的效果呢?可以直接用一个开源的ViewFlow来实现.   项目 ...

  10. Vue 执行npm run dev时报了三个warning的原因

    刚装上了win10,用vue运行下项目,输入npm run dev之后报了三个以下错误: There are multiple modules with names that only differ ...