使用变量初始化的Angular FormControl Validator Min不会随着变量更改而更新

问题描述

我正在使用Angular 8和Material Components,并希望使用最小验证器将表单中的值 b 限制为大于或等于值 a 。我将两个物料输入与一个共享表单控件一起使用,并将它们绑定到 shared 对象的参数。在我的控制器中,我假设 b 的表单控件的验证器定义为min(shared.a),并假设更改了 shared.a ,验证器也应更新。例如,通过输入将 a 设置为4后,除非> = 4,否则 b 应该无效。但是,验证器不会更新对 a的更改,但初始值为 a 似乎是静态的。我想念什么?

example.component.html

<form [formGroup]="exampleForm">
  <mat-form-field>
    <input matInput type=number formControlName="a" [(ngModel)]="shared.a">
  </mat-form-field>

  <mat-form-field class="example-full-width">
    <input matInput type=number formControlName="b" [(ngModel)]="shared.b">
  </mat-form-field>
</form>

example.component.ts

export class Example implements OnInit {
  shared = {'a': 0,'b': 1}
  exampleForm: FormGroup;

  constructor(private formBuilder: FormBuilder){ }

  ngOnInit() {
    this.exampleForm = this.formBuilder.group({
      a: [this.shared.a,Validators.min(0)],b: [this.shared.b,Validators.min(this.shared.a)]
    })
  }
}

Minimal working example on Stackblitz

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)