在Angular 5中,在特定索引

时间:2018-04-12 12:13:20

标签: angular typescript angular-reactive-forms

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

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

这里,插入值是这样的,但是当插入像这个键一样从数组中删除时,这里是下面的方法:

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

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

1 个答案:

答案 0 :(得分:0)

使用任何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);
}