[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)是一个软件设计模式,处理 ...
随机推荐
- Windows SubSystem for Linux(WSL)设置默认和设置默认登陆用户
使用wslconfig命令进行管理 1. 设置默认运行的linux系统 wslconfig /setdefault <DistributionName> 正如上面所说,如果执行wslco ...
- STL || HDU 1894 String Compare
如果一个词包含再另一个词的前面(前缀),是一对前缀,求一共有多少对 *解法:STL万岁 #include<string>:https://www.cnblogs.com/SZxiaochu ...
- EBS ORACLE工单齐套率的计算程序
PROCEDURE Get_wip_accept_item_date(p_use_id in number, p_org_id IN NUMBER, p_start_date IN DATE, p_e ...
- zabbix auto discovery
1.configuration>discovery>create discovery rule ip range:192.168.43.2-254 check: http 80 2.con ...
- Java中的枚举--Enumeration
之前并没有注意到枚举这个知识点,因为之前在项目中并没有使用过枚举,可能是项目并不是很复杂的原因吧,今天看张孝祥老师的讲解,觉得,这个枚举真的有很多值得学习的地方,探究一下枚举的设计原理,底层到底是怎么 ...
- navicate备份与还原数据库
参考:https://jingyan.baidu.com/article/574c521977dea06c8d9dc1b1.html 1.备份数据库 首先新建数据库 选中数据库下方的“备份”,右击“新 ...
- 35个Redis面试题
1.什么是redis? Redis 是一个基于内存的高性能key-value数据库. 2.Reids的特点 Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库 ...
- Global Round 2
A - Ilya and a Colorful Walk CodeForces - 1119A Ilya lives in a beautiful city of Chordalsk. There a ...
- 解决 【xshell 5 不能使用退格键和Delete建】的问题
###按照图片操作即可 1,打开[文件],选择[打开]选项 2.在会话中,打开[属性] 3.点击左边[终端]下的[键盘]选项,按照如下设置 即可.
- github 获取 token
登录github 地址:https://github.com 点击settings 在点击Developer settings 继续 继续 描述栏随意写 复选框是token的权限 都选上吧 点击红框 ...