Allow more than one child component of the same type. Allow child components to be placed within the views of other custom components.

In previous post, we have seen how to use @ContentChild to asscomplish compound component implementation.

It has some limitations, for example,

  1. <toggle (toggle)="onToggle($event)">
  2. <toggle-on>On</toggle-on>
  3. <toggle-off>Off</toggle-off>
  4. <!-- Does not work when has multi children components -->
  5. <toggle-on>On</toggle-on>
  6. <toggle-off>Off</toggle-off>
  7.  
  8. <!-- Does not work when there are some nested component -->
  9. <other-component></other-component>
  10. <toggle-button></toggle-button>
  11. </toggle>

The reason for that is @ContentChild is only looking for the first matched child component, so that the only first child component get updated. Of course, we can use @ContentChildren with QueryList to solve the problem for multi child components, but @ContentChildren doesn't help with nested component.

So the solution is using Angular dependency injection, we want to inject 'ToggleComponent' into its child component. So that from the Child component, we can reference toggle component' state. For example:

toggle.on.component.ts:

  1. import { Component } from '@angular/core';
  2.  
  3. import { ToggleComponent } from './toggle.component';
  4.  
  5. @Component({
  6. selector: 'toggle-on',
  7. template: '<ng-content *ngIf="toggle.on"></ng-content>',
  8. })
  9. export class ToggleOnComponent {
  10. constructor(public toggle: ToggleComponent) { }
  11. }

We inject 'ToggleComponent' and inside the template, we reference toggle.on state form ToggleComponent.

[Angular] Communicate Between Components Using Angular Dependency Injection的更多相关文章

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

  2. [Angular] Component's dependency injection

    An Angular service registered on the NgModule is globally visible on the entire application. Moreove ...

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

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

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

  6. angular源码分析:angular中的依赖注入式如何实现的

    一.准备 angular的源码一份,我这里使用的是v1.4.7.源码的获取,请参考我另一篇博文:angular源码分析:angular源代码的获取与编译环境安装 二.什么是依赖注入 据我所知,依赖注入 ...

  7. 【原创教程】一、Angular教程系列之认识angular

    为什么我会准备写这个原创教程系列? 写下这个标题之后,看着屏幕上空白的内容区,不知从何下手,想说的似乎有很多,似乎又没啥说的.有时候就会陷入这种矛盾中,有时候就是这样,于是,我下定决心这一次一定要把这 ...

  8. 依赖注入 | Dependency Injection

    原文链接: Angular Dependency Injection翻译人员: 铁锚翻译时间: 2014年02月10日说明: 译者认为,本文中所有名词性的"依赖" 都可以理解为 & ...

  9. AngularJs学习笔记--Dependency Injection(DI,依赖注入)

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/di 一.Dependency Injection(依赖注入) 依赖注入(DI)是一个软件设计模式,处理 ...

随机推荐

  1. github 从一个仓库换到另一个仓库

    1.找到.git目录   2.打开config文件 3.修改仓库地址 4.重新提交 git push --all origin 这样就替我们的项目换仓啦!!!^_^

  2. JavaSE-30 BigDecimal类的使用

    问题 Java(其他编程语言也存在类似问题)中浮点数直接进行算术运算会导致精度丢失. 示例代码: System.out.println("1.0 - 0.9 =" + (1.0 - ...

  3. VW结合rem进行移动端布局

    ---恢复内容开始--- html { font-size:10vw: } div { width: 1rem; height: 1rem; } VW这个单位适合用来适应不同设备的 一个设备的宽度就为 ...

  4. FastNet C++/Python 网络通信库之 协议

    协议可以使用的基础数据类型: UInt8,UInt16,UInt32,UInt64Int8,Int16,Int32,Int64Float,Double,Bool,String [T]  数组,T代表元 ...

  5. 智能指针unqiue_ptr

    unique_ptr unique_ptr 对它指向的对象在同一时刻是独占的.它要么在构造的时候使用内置指针初始化,要么使用reset给其赋值.当unique_ptr被销毁时,它所指向的对象也被销毁. ...

  6. 21. SCHEMATA

    21. SCHEMATA 在MySQL中,SCHEMA是数据库,因此SCHEMATA表提供有关数据库的信息. SCHEMATA表有以下列: CATALOG_NAME :SCHEMA所属目录的名称.该值 ...

  7. python基础教程之pymongo库

    1. 引入 在这里我们来看一下Python3下MongoDB的存储操作,在本节开始之前请确保你已经安装好了MongoDB并启动了其服务,另外安装好了Python的PyMongo库. 1.  安装 pi ...

  8. Django之学员管理二

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

  9. 【转】反向AJAX

    原文链接:http://blog.csdn.net/lccone/article/details/7743886 反向Ajax的基本概念是客户端不必从服务器获取信息,服务器会把相关信息直接推送到客户端 ...

  10. 大数据学习——HADOOP集群搭建

    4.1 HADOOP集群搭建 4.1.1集群简介 HADOOP集群具体来说包含两个集群:HDFS集群和YARN集群,两者逻辑上分离,但物理上常在一起 HDFS集群: 负责海量数据的存储,集群中的角色主 ...