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的更多相关文章

  1. [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 ...

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

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

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

  4. 在angular项目中使用web-component ----How to use Web Components with Angular

    原文: https://medium.com/@jorgecasar/how-to-use-web-components-with-angular-41412f0bced8 ------------- ...

  5. angular源码分析:angular中脏活累活的承担者之$interpolate

    一.首先抛出两个问题 问题一:在angular中我们绑定数据最基本的方式是用两个大括号将$scope的变量包裹起来,那么如果想将大括号换成其他什么符号,比如换成[{与}],可不可以呢,如果可以在哪里配 ...

  6. angular源码分析:angular中入境检察官$sce

    一.ng-bing-html指令问题 需求:我需要将一个变量$scope.x = '<a href="http://www.cnblogs.com/web2-developer/&qu ...

  7. angular的跨域(angular百度下拉提示模拟)和angular选项卡

    1.angular中$http的服务: $http.get(url,{params:{参数}}).success().error(); $http.post(url,{params:{参数}}).su ...

  8. angular源码分析:angular的整个加载流程

    在前面,我们讲了angular的目录结构.JQLite以及依赖注入的实现,在这一期中我们将重点分析angular的整个框架的加载流程. 一.从源代码的编译顺序开始 下面是我们在目录结构哪一期理出的an ...

  9. angular源码分析:angular中jqLite的实现——你可以丢掉jQuery了

    一.从function JQLite(element)函数开始. function JQLite(element) { if (element instanceof JQLite) { //情况1 r ...

随机推荐

  1. .NET多线程总结

    1.不需要传递参数,也不需要返回参数 我们知道启动一个线程最直观的办法是使用Thread类,具体步骤如下: public void test() { ThreadStart threadStart = ...

  2. django 模板中{%for%}的使用

    1.{%for athlete in list reversed%}  reversed用于反向迭代 2.for 标签 支持一个可选的 empty 变量 3.forloop 模板变量 4.forloo ...

  3. Element UI tree 回显问题

    Part.1 问题 写项目时遇到一个棘手的问题,在做关于权限功能时,点击修改需要显示角色原本对应的权限.涉及到了 tree 组件回显,但是有一个很尴尬的问题:tree 组件只要父节点选中,那么子节点就 ...

  4. iOS开发基础知识

    1:App跳转至系统Settings 跳转在IOS8以上跟以下是有区别的,如果是IOS8以上可以如下设置: NSURL *url = [NSURL URLWithString:UIApplicatio ...

  5. navicate备份与还原数据库

    参考:https://jingyan.baidu.com/article/574c521977dea06c8d9dc1b1.html 1.备份数据库 首先新建数据库 选中数据库下方的“备份”,右击“新 ...

  6. 模拟、字符串--P1042 乒乓球 题解

    P1042 乒乓球 字符串string的基本使用 #include <iostream> #include <algorithm> #include <map> # ...

  7. (15) openssl签署和自签署证书的多种实现方式

    1.采用自定义配置文件的实现方法 1.1 自建CA 自建CA的机制:1.生成私钥:2.创建证书请求:3.使用私钥对证书请求签名. 由于测试环境,所以自建的CA只能是根CA. 所使用的配置文件如下: [ ...

  8. Django之学员管理二

    Django之学员管理二 学生表的一对多的增删改查 views.py def students(request): #select students.sid,students.name,classes ...

  9. luogu2261 [CQOI2007]余数求和

    除法分块. 猜想: 记 \(g(x)=\lfloor k / \lfloor k / x\rfloor \rfloor\),则对于 \(i \in [x,g(x)]\),\(\lfloor k / i ...

  10. URL 路由

    一般情况下,一个 URL 字符串和它对应的控制器中类和方法是一一对应的关系. URL 中的每一段通常遵循下面的规则: example.com/class/function/id/ 但是有时候,你可能想 ...