在 Angular 5 中,在特定索引处的反应形式数组中插入值

In Angular 5, insert value in reactive forms array at specific index

在这里,我使用的是响应式表单数组,我无法将 QuestionType 值插入表单数组中的特定索引处。下面是数组的 JSON 视图:

{
  "SurveyName": "",
  "Questions": [
    {
      "QuestionName": "",
      "QuestionType": "",
      "AnswersList": [
        {
          "Answer": ""
        }
      ]
    }
  ]
}

这里是用这种方式插入的值,但是,当我插入时,键是从数组中删除的,这里是下面的方法:

selectType(ID: number,event,index) {
    this.QuestionType = ID;
    this.SurveyForm.value.Questions[index].QuestionType = ID;
  }

在数组的任意索引处插入值是动态形式,所以请建议我解决这个问题。

用任意FormControlyou can call the setValue method更新当前值。

如果您只是想在表单中设置一个值,并假设表单控件名称与 JSON 属性相同:

selectType(ID: number,event,index) {
    this.QuestionType = ID;
    this.SurveyForm.controls['Questions'].controls[index].controls['QuestionType'].setValue(ID);
}