[Angular] Configurable Angular Components - Content Projection and Input Templates
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的更多相关文章
- [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 ...
- [Angular] Learn Angular Multi-Slot Content Projection
Now for au-modal component, we pass in tow component though contenct projection: <au-modal class= ...
- [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 ...
- 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 ...
- [Angular 2] Angular 2 Smart Components vs Presentation Components
Both Smart Components and Presentation Components receive data from Services in entirely different w ...
- angular 2 angular quick start Could not find HammerJS
Angular2 的material中 引用了 hammerjs,遇到Could not find HammerJS错误,正确的步骤如下: 需要在如下位置增加 对material 和 hammerjs ...
- ASP.NET Core 2.1 Web API + Identity Server 4 + Angular 6 + Angular Material 实战小项目视频
视频简介 ASP.NET Core Web API + Angular 6的教学视频 我是后端开发人员, 前端的Angular部分讲的比较差一些, 可以直接看代码!!!! 这是一个小项目的实战视频, ...
- [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 ...
- angular自定义指令解决IE89不支持input的placeholder属性
下面代码实测通过,直接copy到本地运行即可. <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...
随机推荐
- 项目: 基于Python socket模块实现的简单 ftp 项目:
需要 自己创建一个 info 文件 用来存储用户信息 服务器: import socket import pickle import struct import os import time ''.s ...
- Impala管理
这里, 以后更新. Impala的安装(含使用CM安装 和 手动安装)(图文详解) 可以通过下面的链接来访问Impala的监护管理页面: • 查看StateStore – http://node1:2 ...
- centos配置tomcat编辑修改
https://jingyan.baidu.com/article/6525d4b1382f0aac7d2e9421.html
- hello world! hello cnbog
第一次开通博客,以后见证我的成长吧!
- touch---创建文件或更改文件日期
- CMDB学习之四 ——DEBUG模式
定义一个debug,进行解析调试,到测试文件 配置文件,配置debug模式,定义环境变量, #!/usr/bin/env python # -*- coding:utf-8 -*- import os ...
- Spring项目用junit 时出现org.junit.runners.BlockJUnit4ClassRunner cannot be resolved(转)
spring框架项目用junit做测试时,程序在自动编译时出现下述问题: 程序的问题是项目中找不到org.junit.runners.BlockJUnit4ClassRunner,有两种可能,一是没有 ...
- unity3d编程日志
2014/4/27 编写脚本的时候,加入了中文凝视,发现console面板有非常多不可思议的bug.查了一下发现是由于monodevelop脚本中文凝视报错,而英文凝视不会受影响. 解决方法:把凝视放 ...
- Android学习笔记进阶十一图片动画播放(AnimationDrawable)
大家平时见到的最多的可能就是Frame动画了,Android中当然也少不了它.它的使用更加简单,只需要创建一个 AnimationDrawabledF对象来表示Frame动画,然后通过addFrame ...
- 4.dubbo-demo+简易监控中心安装+管理控制台安装
转自:https://blog.csdn.net/zhangweigangweiwu/article/details/52244099 tomcat:apache-tomcat-6.0.39 需要用到 ...