为了拥有一个可以监听其包含的表单的有效性状态的变化的组件并执行某些组件的方法,是
reactive forms的方法吗?
使用templateRef(例如[disabled] =“#myForm.invalid”)很容易禁用模板中的提交按钮,但这不涉及组件的逻辑.
看着template forms’ doc我找不到办法
如果您只想获取状态而不是值,则可以使用statusChanges
export class Component {
@ViewChild('myForm') myForm;
this.myForm.statusChanges.subscribe(
result => console.log(result)
);
}
如果您甚至想要更改数据,您可以订阅表单的valueChanges并使用this.myForm.status检查表单的状态
export class Component {
@ViewChild('myForm') myForm;
this.myForm.valueChanges.subscribe(
result => console.log(this.myForm.status)
);
}
状态的可能值为VALID,INVALID,PENDING或disABLED.
Here is the reference for the same