相关疑难解决方法(0)

表单组值更改时不会触发更改检测

我创建了一个简单的示例来演示我面临的一个奇怪问题。

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)

angular angular-changedetection

6
推荐指数
2
解决办法
4403
查看次数

标签 统计

angular ×1

angular-changedetection ×1