尝试在每次调用函数时将CURRENT_TEXT值设置为空字符串“”,但总是获取先前选择的值,知道执行了什么错误吗?
主要
constructor(private dataService: ApiService, private eventService: EventService, private formBuilder: FormBuilder) {
this.questionForm = this.formBuilder.group({
alrgyDetls: formBuilder.group({
ALLERGY_DETAILS: ['']
}),
curntText: this.formBuilder.group({
CURRENT_TEXT: ['']
})
});
}
formatSubQuestions(e: any) {
const answerOption = [{
"answerOptionId": 0,
"answerText": "",
"answerOptionId2": 0
}];
this.questionForm.currentText.patchValue({
CURRENT_TEXT: " "
});
if (e) {
answerOption[0].answerText = this.questionForm.get('curntText.CURRENT_TEXT').value || "";
}
}
}
最佳答案
嗨,您是否真的要在表单中包含两个formGroups,也许您想拥有两个表单控件。
但对于您的示例,您可以通过以下方式进行操作:
formatSubQuestions(e: any) {
const answerOption = [{
"answerOptionId": 0,
"answerText": "",
"answerOptionId2": 0
}];
const formControl = this.questionForm.get('curntText').get('CURRENT_TEXT'); // get the wanted form control
const oldValue = formControl.value; // save the old value before clearing it
formControl.patchValue(''); // clear its value
if (e) {
answerOption[0].answerText = oldValue || '';
}
}