我在表单对象parentForm
中有一个名为'question1'
的表单控件,我已按以下方式订阅了它.
这是一个带有Yes
和No
两个选项的单选按钮,当我 Select No
时,我得到Yes
,当我 Select Yes
时,我得到No
.
this.parentForm.controls['question1'].valueChanges.subscribe(
(selectedValue) => {
// If option `No is selected`
console.log(selectedValue); // displays No. OK
console.log(this.parentForm.value['question1']); // displays Yes. Problem is here
}
);
变量selectedValue
的值是正确的,但是如果我取console.log(this.parentForm.value['question1']
,它会给出之前的值.
在从this.parentForm.value['question1']
中检索值之前,我试着输入setTimeout()
,结果很好.
setTimeout(() => {
console.log(this.parentForm.value['question1']); // gives the correct value.
}, 500);
但我的问题是,为什么parentForm
在其控件的值更改时不更新,而且我也只是在值更改后才检索其值.
注:我不想遵守parentForm.valueChanges
年,这不是我的要求.