ReactiveForm用于动态更新的表单条目

时间:2018-07-02 13:08:29

标签: html angular angular-reactive-forms reactive-forms

如何检查组件中myform的状态以检查是否所有字段都已填充?

HTML:

<div  [formGroup]="myform">
  <div *ngFor="let question of questions">
      <label>{{question.question}}</label>
                <select required >
                  <option selected disabled="disabled">Option</option>
                   <option *ngFor="let option of question['options']">{{option}}</option>
                </select>
            </div>
        </div>
</div>

来自API的问题JSON:

 this.questions =  [
        {
          question: 'What is your age?',
          options: ['20', '30', '40']
        },

        {
          question: 'How much quantity?',
          options: ['1','2','3']
        }]

2 个答案:

答案 0 :(得分:0)

如果使用ReactiveForm,则需要使用FormArray FormArray可以是FormControl或FormGroup

FormControl的FormArray

constructor(private fb:FormBuilder) {}
ngOnInit() {
    //We create an array of FormControl, each question a FormControl
    let data:FormControl[]=this.questions.map(q=>new FormControl());

    this.myform=this.fb.group({
      questions:new FormArray(data)
    })
}
//the .html
<!--we use *ngIf to show the form only when we create the form-->
<div *ngIf="myform"  [formGroup]="myform">
  <!--we iterate to myForm.get('questions').controls -->
  <!--we use our variable "questions" to show the label and options-->
  <div *ngFor="let question of myform.get('questions').controls;let i=index">
      <label>{{questions[i].question}}</label>
      <select required [formControl]="question" >
         <option value="null" disabled="disabled">Option</option>
         <option *ngFor="let option of questions[i].options">{{option}}</option>
      </select>
  </div>
</div>
<!--just for check-->
{{myform?.value |json}}

如果我们使用formGroup的数组,我们将进行一些更改

constructor(private fb:FormBuilder) {}
ngOnInit() {
    //we create and array of FormGroup
    let data2:FormGroup[]=this.questions.map(q=>this.fb.group({
      option:null
    }));

    this.myform2=this.fb.group({
      questions:new FormArray(data2)
    })
}
<div *ngIf="myform2"  [formGroup]="myform2">
  <!--see that we say to Angular the "formArrayName" -->
  <div formArrayName="questions">
    <div *ngFor="let question of myform2.get('questions').controls;
        let i=index" [formGroupName]="i"> <!--don't forget formGroupName-->
        <label>{{questions[i].question}}</label>
        <!--the select use formControlName, our array is an array of FormGroup-->
        <select required formControlName="option" >
           <option value="null" disabled="disabled">Option</option>
           <option *ngFor="let option of questions[i].options">{{option}}</option>
        </select>
    </div>
  </div>
  </div>
  {{myform2?.value |json}}

声明:@FrontEndDeveloper。一件事是我们用来提出问题的数组问题。(也许我必须为变量选择其他名称),另一件事是表单的值。 myform1 = {questions:[“ 20”,“ 1”]}的值,myform2 = {questions:[{option:“ 20”},{option:“ 2”}]]}。

当我们创建一个使用Map的FormControl数组(或FbGroup数组)时,我同样可以做类似的事情

let data:FormControl[]=[];
data.push(new FormControl());
data.push(new FormControl());

let data2:FormGroup[]=[];
data2.push(this.fb.group({
          option:null
        }));
data2.push(this.fb.group({
          option:null
        }));

通常,我们有一些数据来初始化表格。 (带有一些数据的对象)是从dbs中获得的

//Imagine we have mydata{name:"A",options=["20","1"]}
//we can map this data to create the form
let data:FormControl[]=this.mydata.options.map(q=>new FormControl(q));
//or
   let data2:FormGroup[]=this.mydata.options.map(q=>this.fb.group({
          option:q
        }));

//Imagine we have mydata{name:"A",options=[{option:"20"},{option:"1"}]}
//we can map this data to create the form
let data:FormControl[]=this.mydata.options.map(q=>new FormControl(q.option));
//or
   let data2:FormGroup[]=this.mydata.options.map(q=>this.fb.group({
          option:q.option
        }));

答案 1 :(得分:-1)

这将帮助您了解响应式表单的基本功能。

https://stackblitz.com/edit/objects-equality-check-edhyk5?file=src/app/app.component.ts

这将有助于理解: 1. FormBulder, 2. FormGroup, 3.表单值更改等。