[Angular] @ContentChild and ngAfterContentInit
@ContentChild normally works with ngAfterContentInit lifecycle.
@ContentChild is used for looking into child component's props.
For example, we a app component:
<auth-form
(submitted)="loginUser($event)">
<h3>Login</h3>
<auth-remember
(checked)="rememberUser($event)">
</auth-remember>
<button type="submit">
Login
</button>
</auth-form>
Inside it define a 'auth-form' component, and for auth-form component it has 'h3', 'auth-remember' and 'button' component as children.
Auth-form:
<div>
<form (ngSubmit)="onSubmit(form.value)" #form="ngForm">
<ng-content select="h3"></ng-content>
<label>
Email address
<input type="email" name="email" ngModel>
</label>
<label>
Password
<input type="password" name="password" ngModel>
</label>
<ng-content select="auth-remember"></ng-content>
<ng-content select="button"></ng-content>
</form>
</div>
Inside auth-form, it use <ng-content> (content projection) to show 'h3, button & auth-remember' component. So what we want to do here is "inside auth-form component, listen to auth-remember's checked event, using its value to toggle a message div".
Add a message div:
<div>
<form (ngSubmit)="onSubmit(form.value)" #form="ngForm">
<ng-content select="h3"></ng-content>
<label>
Email address
<input type="email" name="email" ngModel>
</label>
<label>
Password
<input type="password" name="password" ngModel>
</label>
<ng-content select="auth-remember"></ng-content>
<div *ngIf="showMessage">You account will be kept for 30 days</div>
<ng-content select="button"></ng-content>
</form>
</div>
Using @ContentChild to get the component object.
import { Component, Output, EventEmitter, AfterContentInit, ContentChild } from '@angular/core';
import { User } from './auth-form.interface';
import {AuthRememberComponent} from './auth-remember.component';
@Component({
selector: 'auth-form',
template: `
...
`
})
export class AuthFormComponent implements AfterContentInit{
showMessage: boolean;
@ContentChild(AuthRememberComponent) remember: AuthRememberComponent;
@Output() submitted: EventEmitter<User> = new EventEmitter<User>();
onSubmit(value: User) {
this.submitted.emit(value);
}
ngAfterContentInit(): void {
if(this.remember) {
this.remember.checked.subscribe((checked: boolean) => {
this.showMessage = checked;
})
}
}
}
If we check 'this.remember':

We can subscribe 'this.remember.checked' to get whether 'auth-remember' is checeked or not, and assign the value to 'showMessage' var.
@ContentChild is really powerful, it can get any props on the child component.
For exmaple, we can add an @Input value and a private prop on to the auth-remember component.
import { Component, Output, EventEmitter, Input } from '@angular/core';
@Component({
selector: 'auth-remember',
template: `
<label>
<input type="checkbox" (change)="onChecked($event.target.checked)">
Keep me logged in
</label>
`
})
export class AuthRememberComponent {
@Output() checked: EventEmitter<boolean> = new EventEmitter<boolean>();
@Input() role: string;
myName: string = "Auth-remember";
onChecked(value: boolean) {
this.checked.emit(value);
}
}
And from the console log, we can see, we get everthing about the auth-remember component.

[Angular] @ContentChild and ngAfterContentInit的更多相关文章
- Angular ContentChild
contentchild // 使用方法 git clone https://git.oschina.net/mumu-osc/learn-component.git cd learn-compone ...
- [Angular] @ContentChild with Directive ref
For example you have a component, which take a trasclude input element: <au-fa-input id="pas ...
- ViewChild与ContentChild的联系和区别
原文 https://www.jianshu.com/p/5ab619e576ea 大纲 1.认识ViewChild 2.认识ContentChild 3.ViewChild与ContentChild ...
- [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] Difference between ngAfterViewInit and ngAfterContentInit
Content is what is passed as children. View is the template of the current component. The view is in ...
- [Angular] Difference between ViewChild and ContentChild
*The children element which are located inside of its template of a component are called *view child ...
- angular ViewChild ContentChild 系列的查询参数
官方说明 官方文档 在调用 NgAfterViewInit 回调函数之前就会设置这些视图查询. 元数据属性: selector - 用于查询的指令类型或名字. read - 从查询到的元素中读取另一个 ...
- Angular开发实践(三):剖析Angular Component
Web Component 在介绍Angular Component之前,我们先简单了解下W3C Web Components 定义 W3C为统一组件化标准方式,提出Web Component的标准. ...
- angular5 @viewChild @ContentChild ElementRef renderer2
@viewChild 作用一:选择组件内节点 <!--视图 --> <div #mydiv><input></div> // 选择 @ViewChild ...
随机推荐
- android开发者要懂得问题答案
我在网上看了一下有些人在博客上提出一些什么android开发者必须懂得问题,可是就是没有答案,所以我就把这些问题拷贝过来了.顺便也把全部的答案加上,为了让很多其它的开发者高速的找到答案,谢谢! 以下的 ...
- 为什么golang的开发效率高(编译型的强类型语言、工程角度高、在开发上的高效率主要来自于后发优势,编译快、避免趁编译时间找产品妹妹搭讪,既是强类型语言又有gc,只要通过编译,非业务毛病就很少了)
作者:阿猫链接:https://www.zhihu.com/question/21098952/answer/21813840来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...
- 微信支付v2开发(11) Native支付
关键字:微信公众平台 微信支付 Native原生支付 作者:方倍工作室 原文:http://www.cnblogs.com/txw1958/p/wxpay-native.html 在这篇微信公众平台开 ...
- C语言库函数,头文件
参看:https://zhidao.baidu.com/question/328173842.html 该文件包含了的C语言标准库函数的定义 stdlib.h里面定义了五种类型.一些宏和通用工具函数. ...
- 软件——protel 的pcb电路图制作
近期一直在学习PCB板的绘制.
- 10559 - Blocks(方块消除|DP)
该题乍一看和矩阵链乘非常类似,但是有一个不同之处就是该题能够拼接 . 为了达到这个目的.我们不得不拓展维度d[i][j][k].用一个k表示最右边拼接了k个和a[j]同样颜色的方块. 问题的关键在 ...
- php-wamp环境搭建
wamp(Windows,Apache,Mysql,PHP) win8.1下搭建apache2.4(64位) php5.6.11(64位) mysql5.6.24(32位) d盘创建文件结构为: ...
- 原生js大总结一
001.浅谈堆和栈的理解? js变量存储有栈存储和堆存储,基本数据类型的变量存储在栈中,引用数据类型的变量存储在堆中 引用类型数据的地址也存在栈中 当访问基础类型变量时,直接从栈中取值.当访问 ...
- Maven学习总结(17)——深入理解maven灵活的构建
一个优秀的构建系统必须足够灵活,应该能够让项目在不同的环境下都能成功构建.maven为了支持构建的灵活性,内置了三大特性,即:属性.profile和资源过滤. 1.maven属性 maven属性分6类 ...
- Java中关于static语句块的理解
Java中关于static语句块的理解 一.static块会在类被加载的时候执行且仅会被执行一次,一般用来初始化静态变量和调用静态方法. 实例一 public class A{ String name ...