[Angular] @ViewChildren and querylists (ngafterviewinit)

Source: Internet
Author: User

When your use @ViewChildren, the value can is only accessable inside ngafterviewinit lifecycle. This was somehow different from @ViewChild, which value can 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-contentSelect="H3"></ng-content> <label>Email Address<input type="Email"Name="Email"Ngmodel> </label> <label>Password<input type="Password"Name="Password"Ngmodel> </label> <ng-contentSelect="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-contentSelect="Button"></ng-content> </form> </div>  `}) ExportclassAuthformcomponent implements Aftercontentinit, Afterviewinit {showmessage:boolean; @ViewChildren (authmessagecomponent) message:querylist <AuthMessageComponent>; @ContentChildren (authremembercomponent) remember:querylist<AuthRememberComponent>; @Output () Submitted:eventemitter<User> =NewEventemitter<user>(); Constructor (Privatecd: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 are 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 on production mode, only in development mode.

[Angular] @ViewChildren and querylists (ngafterviewinit)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.