这里,我使用的是被动表单数组,无法插入 试题类型 值放入特定索引处的表单数组中。下面是数组的JSON视图:
{ "SurveyName": "", "Questions": [ { "QuestionName": "", "QuestionType": "", "AnswersList": [ { "Answer": "" } ] } ] }
这里是以这种方式插入的值,但是,当我像那样插入时,从数组中移除键,下面是方法:
selectType(ID: number,event,index) { this.QuestionType = ID; this.SurveyForm.value.Questions[index].QuestionType = ID; }
它是在数组的任何索引处插入值的动态形式,因此,请建议我解决此问题。
有任何 FormControl ,则, you can call the setValue method 更新当前值。
FormControl
setValue
如果您只是试图在表单中设置一个值,并假设表单控件名称与JSON属性相同:
selectType(ID: number,event,index) { this.QuestionType = ID; this.SurveyForm.controls['Questions'].controls[index].controls['QuestionType'].setValue(ID); }