zoukankan      html  css  js  c++  java
  • [Angular] @ViewChildren and QueryLists (ngAfterViewInit)

    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 = 30;
          });
          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.

  • 相关阅读:
    Mybatis(二) Mybatis通用的写法
    Mybatis(一)Mybatis相关概念
    NodeJS添加Jquery依赖
    安卓、IOS端AEC密钥加密 Java端密钥解密通用实现(16进制表现形式)
    关于博客园首页及详情页美化的代码
    MD5用户密码加密工具类 MD5Util
    .Net Core跨平台应用研究-CustomSerialPort(增强型跨平台串口类库)
    FtpServer穿透内网访问配置踩坑笔记
    .Net Core之编辑json配置文件
    玩转MQTT-阿里云之MQTT使用(下)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6417976.html
Copyright © 2011-2022 走看看