[Angular] Write Compound Components with Angular’s ContentChild
Allow the user to control the view of the toggle component. Break the toggle component up into multiple composable components that can be rearranged by the app developer.
Compound component mainly for rendering flexibility. It hides the implements detial from users, but users can freely re-order the child component order or add new component into it.
In this post we are looking into how to apply "@ConetentChild" to do compound components.
This is not a recommended way, just for learning!
So a highlevel compound component looks like:
<toggle (toggle)="onToggle($event)">
<toggle-button></toggle-button>
<toggle-on>On</toggle-on>
<toggle-off>Off</toggle-off>
</toggle>
For the parent component 'toggle' component, the job for it is hide implement details from the consumers. It handles the internal state. Which means 'toggle' component needs to access the state of its Children components.
<toggle-button></toggle-button>
<toggle-on>On</toggle-on>
<toggle-off>Off</toggle-off>
The way to can accomplish it is using '@ContentChild':
@ContentChild(ToggleOnComponent) toggleOn: ToggleOnComponent;
@ContentChild(ToggleOffComponent) toggleOff: ToggleOffComponent;
@ContentChild(ToggleButtonComponent) toggleButton: ToggleButtonComponent;
Listen for Child component's ouput event:
toggle-button component has Output event call 'toggle':
@Component({
selector: 'toggle-button',
template: '<switch [on]="on" (click)="onClick()" ></switch>',
})
export class ToggleButtonComponent {
@Input() on: boolean;
@Output() toggle: EventEmitter<boolean> = new EventEmitter();
onClick() {
this.on = !this.on;
this.toggle.emit(this.on);
}
}
Then we can listen the Output event in 'ngAfterContentInit()' lifecycle hooks.
ngAfterContentInit() {
this.toggleButton.toggle.subscribe(on => {
this.on = on;
this.toggle.emit(on);
this.update();
});
}
Also 'toggle' component will take care to update Children components state:
update() {
this.toggleOn.on = this.on;
this.toggleOff.on = this.on;
this.toggleButton.on = this.on;
}
[Angular] Write Compound Components with Angular’s ContentChild的更多相关文章
- [Angular] Communicate Between Components Using Angular Dependency Injection
Allow more than one child component of the same type. Allow child components to be placed within the ...
- [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 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项目中使用web-component ----How to use Web Components with Angular
原文: https://medium.com/@jorgecasar/how-to-use-web-components-with-angular-41412f0bced8 ------------- ...
- angular源码分析:angular中脏活累活的承担者之$interpolate
一.首先抛出两个问题 问题一:在angular中我们绑定数据最基本的方式是用两个大括号将$scope的变量包裹起来,那么如果想将大括号换成其他什么符号,比如换成[{与}],可不可以呢,如果可以在哪里配 ...
- angular源码分析:angular中入境检察官$sce
一.ng-bing-html指令问题 需求:我需要将一个变量$scope.x = '<a href="http://www.cnblogs.com/web2-developer/&qu ...
- angular的跨域(angular百度下拉提示模拟)和angular选项卡
1.angular中$http的服务: $http.get(url,{params:{参数}}).success().error(); $http.post(url,{params:{参数}}).su ...
- angular源码分析:angular的整个加载流程
在前面,我们讲了angular的目录结构.JQLite以及依赖注入的实现,在这一期中我们将重点分析angular的整个框架的加载流程. 一.从源代码的编译顺序开始 下面是我们在目录结构哪一期理出的an ...
- angular源码分析:angular中jqLite的实现——你可以丢掉jQuery了
一.从function JQLite(element)函数开始. function JQLite(element) { if (element instanceof JQLite) { //情况1 r ...
随机推荐
- Java集合(四)--基于JDK1.8的ArrayList源码解读
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess ...
- python django项目断点调试
- 搜索 || BFS || POJ 3278 Catch That Cow
农夫在x位置,下一秒可以到x-1, x+1, 2x,问最少多少步可以到k *解法:最少步数bfs 要注意的细节蛮多的,写在注释里了 #include <iostream> #include ...
- c++的if语句中的110为什么不等于110?
从上图可以看出,当表达式1.1*x被直接放进if的判断括号中时1.1*x不等于y,但是将1.1*x赋值给z时,z与y相等,这是为什么?(以下为不等价时的代码) #include<stdio.h& ...
- P2756 网络流解决二分图最大匹配
P2756 飞行员配对方案问题 题目背景 第二次世界大战时期.. 题目描述 P2756 飞行员配对方案问题 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语 ...
- 2018 GDCPC 省赛总结
第二次参加省赛了,对比上年连STL都不会的acm入门者来说, 今年是接触acm的第二年. 首先要说的是今年的省赛比上年人数多了很多, 闭幕式200多支队伍坐满了整个礼堂还要站着不少人,所以今年的竞争其 ...
- Oracle常用内置数据表查询
Oracle 查询库中所有表名.字段名.字段名说明,查询表的数据条数.表名.中文表名. 查询所有表名:select t.table_name from user_tables t;查询所有字段名:se ...
- 服务器端架构及实战 — C#分享
简介 此文是我2008年读研究生期间做的C#项目技术分享,给计算机专业学生的一些经验分享. 当时工作2年后读研. 计算机基础了解及介绍 了解计算机的核心课程和大家的理解 二进制的历史和原理 数字逻辑及 ...
- HDU-3746Cyclic Nacklace,next数组简单应用。
Cyclic Nacklace 节省篇幅不粘题面了... 看懂题后脑袋里略过KMP,学过但没怎么用过,又直接跳下一题了.. 题意:给定一个字符串,可以从两边加上一些字符使其有循环节..求最少需要加多少 ...
- FZU-1881-Problem 1881 三角形问题,打表二分查找~~
B - 三角形问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descripti ...