我创建了一个简单的示例来演示我面临的一个奇怪问题。
Stackblitz- https: //stackblitz.com/edit/angular-change-detection-form-group
我有三个组成部分,它们是:
1-应用程序组件
import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'my-app',
template: `<hello [form]="form"></hello>
<hr />
<button (click)="changeFormValue()">Change Form Value</button>`,
styleUrls: ['./app.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class AppComponent implements OnInit {
name = 'Angular';
form: FormGroup;
ngOnInit() {
this.form = new FormGroup({
name: new FormControl('ABC'),
age: new FormControl('24')
});
}
changeFormValue() {
this.form.setValue({
name: 'XYZ',
age: 35
})
}
}
Run Code Online (Sandbox Code Playgroud)
2-Hello组件
import { Component, Input, OnChanges, ChangeDetectionStrategy …Run Code Online (Sandbox Code Playgroud)