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,

<toggle (toggle)="onToggle($event)">
<toggle-on>On</toggle-on>
<toggle-off>Off</toggle-off>
<!-- Does not work when has multi children components -->
<toggle-on>On</toggle-on>
<toggle-off>Off</toggle-off> <!-- Does not work when there are some nested component -->
<other-component></other-component>
<toggle-button></toggle-button>
</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:

import { Component } from '@angular/core';

import { ToggleComponent } from './toggle.component';

@Component({
selector: 'toggle-on',
template: '<ng-content *ngIf="toggle.on"></ng-content>',
})
export class ToggleOnComponent {
constructor(public toggle: ToggleComponent) { }
}

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. Asp.Net Core 入门(四)—— Model、View、Controller

    和我们学习Asp.Net MVC一样,Asp.Net Core MVC的Model.View.Controller也和我们熟悉的Asp.Net MVC中的相似.不同的是我们在使用Asp.Net Cor ...

  2. Promise 理解与使用

    个人觉得这篇博客写的非常详细且易懂,推荐给小伙伴们~ https://www.cnblogs.com/lvdabao/p/es6-promise-1.html#!comments

  3. JavaScript 非常重要的几个概念

    JavaScript是一门比较复杂的语言.如果你是一名JavaScript开发人员,不管处于什么样的水平,都有必要了解JavaScript的基本概念.小编最近的工作涉及到JavaScript,于是本文 ...

  4. Linux常用命令大全3

    linux命令1,关机shutdown -h now2,init 0 关闭系统3,shutdown -h hours:minutes &按预定时间关闭系统4,shutdown -c取消按预定时 ...

  5. spring注解开发-容器创建全过程(源码)

    1.Spring容器的创建会经历refresh()方法[创建刷新](以AnnotationConfigApplicationContext为例) public AnnotationConfigAppl ...

  6. 从零实现一个http服务器

    我始终觉得,天生的出身很重要,但后天的努力更加重要,所以如今的很多“科班”往往不如后天努力的“非科班”.所以,我们需要重新给“专业”和“专家”下一个定义:所谓专业,就是别人搞你不搞,这就是你的“专业” ...

  7. react native 标签出错.

    这种错误为标签错误,没办法,你只能往标签上找了,但不一定是<Text></Text>,我是在<TextInput></TextInput>上出错的,多了 ...

  8. 基于 WPF + Modern UI 的 公司OA小助手 开发总结

    前言: 距离上一篇博客,整整一个月的时间了.人不能懒下来,必须有个阶段性的总结,算是对我这个阶段的一个反思.人只有在总结的过程中才会发现自己的不足. 公司每天都要在OA系统上上班点击签到,下班点击签退 ...

  9. cherrypy入门

    主要是一个简单的cherrypy hello例子 import cherrypy from jinja2 import Environment, FileSystemLoader import os ...

  10. 大数据学习——yum练习安装jdk

    yum list | grep jdk 安装jdk-1.8.0版本 -openjdk* 安装后,执行java -version 配置环境变量 使用vim /etc/profile 编辑profile文 ...