Change detection does not trigger when the formgroup values change

前端 未结 4 653
我在风中等你
我在风中等你 2021-01-11 19:12

I have created a simple example to demonstrate a weird issue I\'m facing.

Stackblitz - https://stackblitz.com/edit/angular-change-detection-form-group

I hav

4条回答
  •  长发绾君心
    2021-01-11 19:59

    Angular only detects changes if the memory address of the variable changes. Setting the value does not change memory address, thus does not hit ngOnChanges.

    Same goes with arrays. A simple push does not hit ngOnChanges, have to change memory address by = to new array.

    Try this:

    import { Component, Input, OnInit, OnChanges, ChangeDetectionStrategy } from '@angular/core';
    import { FormGroup } from '@angular/forms';
    
    @Component({
      selector: 'app-input',
      template: `
      
    Name : {{form.value.name}}

    Age : {{form.value.age}}
    `, styles: [``], changeDetection: ChangeDetectionStrategy.OnPush }) export class InputComponent implements OnInit, OnChanges { @Input() form: FormGroup; ngOnInit() { } ngOnChanges(changes) { console.log(changes) } }

提交回复
热议问题