[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 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的更多相关文章
- [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 ...
- [Angular] Component's dependency injection
An Angular service registered on the NgModule is globally visible on the entire application. Moreove ...
- [Angular 2] Angular 2 Smart Components vs Presentation Components
Both Smart Components and Presentation Components receive data from Services in entirely different w ...
- 在angular项目中使用web-component ----How to use Web Components with Angular
原文: https://medium.com/@jorgecasar/how-to-use-web-components-with-angular-41412f0bced8 ------------- ...
- 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源码分析:angular中的依赖注入式如何实现的
一.准备 angular的源码一份,我这里使用的是v1.4.7.源码的获取,请参考我另一篇博文:angular源码分析:angular源代码的获取与编译环境安装 二.什么是依赖注入 据我所知,依赖注入 ...
- 【原创教程】一、Angular教程系列之认识angular
为什么我会准备写这个原创教程系列? 写下这个标题之后,看着屏幕上空白的内容区,不知从何下手,想说的似乎有很多,似乎又没啥说的.有时候就会陷入这种矛盾中,有时候就是这样,于是,我下定决心这一次一定要把这 ...
- 依赖注入 | Dependency Injection
原文链接: Angular Dependency Injection翻译人员: 铁锚翻译时间: 2014年02月10日说明: 译者认为,本文中所有名词性的"依赖" 都可以理解为 & ...
- AngularJs学习笔记--Dependency Injection(DI,依赖注入)
原版地址:http://code.angularjs.org/1.0.2/docs/guide/di 一.Dependency Injection(依赖注入) 依赖注入(DI)是一个软件设计模式,处理 ...
随机推荐
- Deep_into_iris
具体ipynb文件请移步Github #各种所需要的库函数首先加载 import numpy as np import pandas as pd import matplotlib.pyplot as ...
- CPP-基础:模板
// template.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <iostream> #include &l ...
- css3新增属性:多列(column)
css3多列能够创建多个列来对文本进行布局,就想报纸那样. 关于多列的相关属性及属性值如下: column-count: number|auto;:指定元素应分为的列数.column-fill: 指定 ...
- eclipse下svn的分支与合并指南 - 更新版
http://wenku.baidu.com/link?url=ul5vzBHZpHgzENp46RQwTYrkCUYLeVg9TuhmPM_qisR1BGzp6Qca7onhS-SOzwDYuYdA ...
- UI进阶 即时通讯之XMPP环境搭建
内容中包含 base64string 图片造成字符过多,拒绝显示
- linux中的vi命令
linux的重要的几个命令如下: ①,光标的操作 1,gg,G,nG,:n gg移到文档的开头一行,G移动到最后一行,nG移动到第n行,到指定的行. 2,H,M,L 光标分别移动到这个界面的最上边,中 ...
- 使用JQuery.slideBox实现图片滚动效果
1.下载JQuery.slideBox和jquery插件,并引用 <link href="css/jquery.slideBox.css" rel="stylesh ...
- Python MySQLdb 查询中文出现问号的解决方法
在连接数据库的时候设置如下即可: db = MySQLdb.connect('localhost','root','×××××','test',use_unicode=True,charset=&qu ...
- NYOJ301-递推求值
递推求值 nyoj上矩阵专题里的10道题水了AC率最高的5道,惭愧,还不是完全自己写的,用了几乎两周的时间.模板题我是有自信写出来的,但对于高级一点的矩阵构造,我还是菜的抠脚. 这题感谢MQL大哥和她 ...
- 【bzoj1090】 [SCOI2003]字符串折叠
[bzoj1090] [SCOI2003]字符串折叠 2014年3月9日3,1140 Description 折叠的定义如下: 1. 一个字符串可以看成它自身的折叠.记作S S 2. X(S)是X ...