When you use @ViewChildren, the value can only be accessable inside ngAfterViewInit lifecycle. This is somehow different from @ViewChild, which value can be accessed from ngAfterContentInit lifecycle.

import { Component, ChangeDetectorRef, Output, ViewChildren, AfterViewInit, EventEmitter, ContentChildren, QueryList, AfterContentInit } from '@angular/core';

import { AuthRememberComponent } from './auth-remember.component';
import { AuthMessageComponent } from './auth-message.component'; import { User } from './auth-form.interface'; @Component({
selector: 'auth-form',
template: `
<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>
<auth-message
[style.display]="(showMessage ? 'inherit' : 'none')">
</auth-message>
<auth-message
[style.display]="(showMessage ? 'inherit' : 'none')">
</auth-message>
<auth-message
[style.display]="(showMessage ? 'inherit' : 'none')">
</auth-message>
<ng-content select="button"></ng-content>
</form>
</div>
`
})
export class AuthFormComponent implements AfterContentInit, AfterViewInit { showMessage: boolean; @ViewChildren(AuthMessageComponent) message: QueryList<AuthMessageComponent>; @ContentChildren(AuthRememberComponent) remember: QueryList<AuthRememberComponent>; @Output() submitted: EventEmitter<User> = new EventEmitter<User>(); constructor(private cd: ChangeDetectorRef) {} ngAfterViewInit() {
console.log("this.message:", this.message); // QueryList {...}
if (this.message) {
this.message.forEach((message) => {
message.days = ;
});
this.cd.detectChanges();
}
} ngAfterContentInit() {
console.log("this.message:", this.message); // undefined
if (this.remember) {
this.remember.forEach((item) => {
item.checked.subscribe((checked: boolean) => this.showMessage = checked);
});
}
} onSubmit(value: User) {
this.submitted.emit(value);
} }

Here we try to modify the value inside ngAfterViewInit lifecycle. but in developement mode, there is change detection error! We cannot modify the 'messages.day' after view init.

We can bypass this problem by using 'ChangeDetectRef'.

this.cd.detectChanges();

To tell Angular change detection everything is fine. And this error won't show up in production mode, only in development mode.

[Angular] @ViewChildren and QueryLists (ngAfterViewInit)的更多相关文章

  1. Angular 2 中的 ViewChild 和 ViewChildren

    https://segmentfault.com/a/1190000008695459 ViewChild ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素.视图查询在 ngAfter ...

  2. Angular 2 ViewChild & ViewChildren

    一.ViewChild ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素.视图查询在 ngAfterViewInit 钩子函数调用前完成,因此在 ngAfterViewInit 钩子函 ...

  3. angular 生命周期钩子 ngOnInit() 和 ngAfterViewInit() 的区别

    angular 生命周期钩子的详细介绍在 https://angular.cn/guide/lifecycle-hooks  文档中做了介绍. ngOnInit() 在 Angular 第一次显示数据 ...

  4. Angular ViewChild & ViewChildren

    基础 ViewChild ViewChild 装饰器用于获取模板视图中的元素或直接调用其组件中的方法.它支持 Type 类型或 string 类型的选择器,同时支持设置 read 查询条件,以获取不同 ...

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

  6. Angular中ViewChild\ngAfterViewInit\Promise的使用,在父组件初始化时等待子组件的返回值

    1.子component中的异步方法 initCreateJob = () => new Promise((resolve, reject) => { setTimeout(() => ...

  7. Angular开发实践(四):组件之间的交互

    在Angular应用开发中,组件可以说是随处可见的.本篇文章将介绍几种常见的组件通讯场景,也就是让两个或多个组件之间交互的方法. 根据数据的传递方向,分为父组件向子组件传递.子组件向父组件传递及通过服 ...

  8. Angular中不同的组件间传值与通信的方法

    主要分为父子组件和非父子组件部分. 父子组件间参数与通讯方法 使用事件通信(EventEmitter,@Output): 场景:可以在父子组件之间进行通信,一般使用在子组件传递消息给父组件: 步骤: ...

  9. Angular使用总结 --- 如何正确的操作DOM

    无奈接手了一个旧项目,上一个老哥在Angular项目中大量使用了JQuery来操作DOM,真的是太不讲究了.那么如何优雅的使用Angular的方式来操作DOM呢? 获取元素 1.ElementRef  ...

随机推荐

  1. leetCode解题报告5道题(十)

    题目一:Valid Number Validate if a given string is numeric. Some examples: "0" => true &quo ...

  2. animation-list -帧动画

    帧动画实现起来比较简单,今天接触到使用xml来创建帧动画,记录下来. 它说白了,其实就是动态的展示图片而已 1.在xml中定义帧动画,如下 <?xml version="1.0&quo ...

  3. 1.1 Introduction中 Kafka as a Messaging System官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka as a Messaging System kafka作为一个消息系统 ...

  4. JQuery操作数组函数 push(),pop(),unshift(),shift()

    1.array.push() :在数组尾部添加新的元素,并返回新的数组长度. 2.array.unshift() :在数组头部添加新的元素,并返回新的数组长度.[听说IE浏览器不支持] 3.array ...

  5. Python 极简教程(六)运算符

    运算符,我们日常生活中使用的加减乘除,都是运算符的一种.当然这种一般我们称为算术运算符,用于处理数字运算的. 但是在计算机语言中,还有很多的运算符.用于处理不用的情况. 主要有以下几类: 算术运算符 ...

  6. ajax上传进度条

    <script type="text/javascript"> function register(){ var frm = document.getElementBy ...

  7. COCOS学习笔记--持续动作ActionInterval

    上一篇博客介绍了即时动作ActionInstant.与即时动作相对的是持续动作ActionInterval. 顾名思义,持续动作就是须要一段时间来持续运行的动作,而且在有限时间内改变运行对象的一些属性 ...

  8. js进阶 13-3 jquery动画显示隐藏,滑动,淡入淡出的本质是什么

    js进阶 13-3 jquery动画显示隐藏,滑动,淡入淡出的本质是什么 一.总结 一句话总结:分别改变display,高度,opacity透明度这三种属性. 1.fade系列函数有哪四个? fade ...

  9. Linux 系统挂载数据盘 分类: B3_LINUX 2015-01-30 18:13 228人阅读 评论(0) 收藏

    适用系统:Linux(Redhat , CentOS,Debian,Ubuntu) *  Linux的云服务器数据盘未做分区和格式化,可以根据以下步骤进行分区以及格式化操作. 下面的操作将会把数据盘划 ...

  10. (十三)RabbitMQ消息队列-VirtualHost与权限管理

    原文:(十三)RabbitMQ消息队列-VirtualHost与权限管理 VirtualHost 像mysql有数据库的概念并且可以指定用户对库和表等操作的权限.那RabbitMQ呢?RabbitMQ ...