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. QT+常见控件+tab Widget 和Stacked Widget

    首先:这里介绍以下tab Widget 和Stacked Widget 之间的区别和使用的方法: tab Widget控件可以直接的进行切换,Stacked Widget却不可以直接在界面上进行切换, ...

  2. sqlserver差异备份3117

    1.出现错误"3117" 2.完整备份/选项/不对数据库执行任何操作 3.数据库显示"正在还原" 4.差异备份/选项/回滚 5.数据库完整备份与差异备份成功

  3. Django框架基础知识14-类视图

    MTV view视图 wsgi函数 def index(request): .... return HttpResponse() 特定的HTTP方法.get,post可以定义单独的方法 继承,多继承, ...

  4. 【HDU 3037】Saving Beans(卢卡斯模板)

    Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...

  5. 实验:iscsi共享存储

    实验名称: iscsi共享存储 实验环境: 我们需要准备一个磁盘,对于这个磁盘我们需要使用,将这个磁盘空间共享给iscsi客户端: 实验需求: 我们这里使用两台服务器来实现iscsi共享存储: 1.指 ...

  6. sql无效字符 执行sql语句报错解决方案

    以为是sql中参数赋值有问题,但是将sql语句直接copy到PLSQL中执行,却没问题,纠结了好久,原来是 insert语句多了:唉,坑爹 http://www.jb51.net/article/32 ...

  7. xtu read problem training 3 B - Gears

    Gears Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 3789 ...

  8. laravel 文件删除

    删除文件 <?php class demo{ public function del(){ $disk = Storage::disk('public');//获取磁盘实例 $disk-> ...

  9. Archiving not possible: No primary destinations errors

    If space ran out in an archive destination, after you fix the problem, you may still recieve the fol ...

  10. [BZOJ3052][UOJ#58][WC2013]糖果公园

    [BZOJ3052][UOJ#58][WC2013]糖果公园 试题描述 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来 ...