angular Docheck

import { Component, OnInit, Input, OnChanges, SimpleChanges, DoCheck } from '@angular/core';

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit, OnChanges, DoCheck {

  @Input()
  greeting: string;

  @Input()
  user: {};

  message = 'hello';

  constructor() { }

  ngOnInit() {
  }

  ngOnChanges(changes: SimpleChanges): void {
    console.log(JSON.stringify(changes, null, 2));
  }

  ngDoCheck(): void {
    console.log('子组件Docheck');
  }

}
<div>子组件</div>
<p>问候语{{greeting}}</p>
<p>用户{{user.name}}</p>
<input [(ngModel)]='message'>
import { Component, DoCheck } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements DoCheck {

  greeting = 'nihao';
  user: { name: string } = { name: 'cys' };

  ngDoCheck(): void {
    console.log('父组件Docheck');
  }
}
<input [(ngModel)]="greeting" type="text">
<input [(ngModel)]="user.name" type="text">
<app-child [greeting]="greeting" [user]="user"></app-child>
原文地址:https://www.cnblogs.com/chenyishi/p/8940734.html