如何在ngFor中使用反应形式

时间:2018-07-27 14:43:40

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

一个用户数组可以具有多个元素(用户)。在模板中,我遍历此数组并为每个元素添加一个表单。现在,每个表单都绑定到相同的表单组值。如果我为第一个用户填写表格,则所有表格的提交按钮都将启用。如何将每个表单指向唯一的formGroup或唯一的变量?

组件:

import { FormControl, FormGroup, Validators, FormBuilder} from '@angular/forms';
...
export class UserComponent implements OnInit {
 form: FormGroup;
 @input users: any[];

 constructor(private _fb: FormBuilder) {}
 ngOnInit() {
  this.form = this._fb.group({
   address: new FormControl('', [Validators.required]),
   phone: new FormControl('', [Validators.required]),
   });
 }

 add() {
   if (this.form.valid){
     // code
   }
 }

模板:

 <div *ngFor="let user if users">
  {{user.name}}
    <form [formGroup]="form"  (ngSubmit)="add()">         
     <div class="form-group">
       <label>Address:</label>
       <input formControlName="address" class="form-control">
       <label>Phone:</label>
       <input formControlName="phone" class="form-control"></input>
     </div>
     <button type="submit" class="submit" [disabled]="!form.valid">Submit</button>    
    </form>
 </div>

3 个答案:

答案 0 :(得分:1)

对于反应式表单,您可以定义数据结构以将表单数据保存在代码中。在您的示例中,您使用this.form对其进行了定义。但是您的表单定义只有一个地址控件和一个电话控件。因此,您已经告诉Angular仅存储一个地址和一部电话。

要允许多个地址/电话机,您需要@DavidZ提到的FormArray。顾名思义,FormArray允许您拥有一组表单值。

在您的示例中,您的FormArray将由一个FormGroup组成,每个用户一个条目。 FormGroup将由一组FormControl组成:地址和电话。

表单组件

此示例具有一个名称和一个地址数组,但应给您一个大致的概念:

ngOnInit(): void {
    this.customerForm = this.fb.group({
        name: ['', [Validators.required, Validators.minLength(3)]],
        addresses: this.fb.array([this.buildAddress()])
    });
}

buildAddress(): FormGroup {
    return this.fb.group({
            addressType: 'home',
            street1: ['', Validators.required],
            street2: '',
            city: '',
            state: '',
            zip: ''
    });
}

您可以在此处找到完整的代码:https://github.com/DeborahK/Angular2-ReactiveForms/tree/master/Demo-Final-Updated

答案 1 :(得分:1)

要解决您的问题,您必须按以下方式使用formArray

在组件中:

export class UserComponent implements OnInit {
     usersForm: FormGroup;
     errorMessage : string;

     constructor(private formBuilder: FormBuilder) {}

      ngOnInit() {
          this.usersForm= this.formBuilder.group({
               users: this.formBuilder.array([
                this.formBuilder.group({
                  address: [null, [Validators.required]],
                  phone: [null, [Validators.required]]
                })
              ])
           });
       }

      initUserRow(): FormGroup {
            return this.formBuilder.group({
               address: [null, [Validators.required]],
               phone: [null, [Validators.required]],
        });
       }

       addUserRow(): void {
          const usersArray= 
             <FormArray>this.usersForm.controls['users'];
           usersArray.push(this.initUserRow());
        }

       removeUserRow(rowIndex: number): void {
           const usersArray= <FormArray>this.usersForm.controls['users'];
           if (usersArray.length > 1) {
              usersArray.removeAt(rowIndex);
           } else {
            this.errorMessage = 'You cannot delete this row! form should contain at least one row!';
           setTimeout(() => {
             this.errorMessage = null;
           }, 4000);
         }
       }

 }

在视图中:

<form *ngIf="usersForm" [formGroup]="usersForm" (ngSubmit)="createUsers()">
    <table class="table table-sm table-bordered">
            <thead>
              <tr class="text-center">
                <th>Address</th>
                <th>Phone</th>
                <th>Action</th>
              </tr>
            </thead>
            <tbody formArrayName="users">
              <tr *ngFor="let item of usersForm.controls.users.controls; let $index=index" [formGroupName]="$index">
                <td style="min-width: 120px">
                      <input class="form-control" type="text" formControlName="address"/>
                      <div class="text-danger" *ngIf="usersForm.controls['users'].controls[$index].controls['address'].touched
                         && usersForm.controls['users'].controls[$index].controls['address'].hasError('required')">
                        Please enter address!
                      </div>
                </td>

                <td style="min-width: 120px">
                      <input class="form-control" type="text" formControlName="address"/>
                      <div class="text-danger" *ngIf="usersForm.controls['users'].controls[$index].controls['phone'].touched
                         && usersForm.controls['users'].controls[$index].controls['phone'].hasError('required')">
                        Please enter phone number!
                      </div>
                </td>

                <td style="width: 100px">
                  <button (click)="addUserRow()" class="btn btn-success btn-sm mr-1" type="button"><i class="fa fa-plus"></i></button>
                  <button (click)="removeUserRow($index)" class="btn btn-danger btn-sm" type="button"><i class="fa fa-times"></i></button>
                </td>
              </tr>
            </tbody>
          </table>
</form>

希望现在您的问题将得到解决!

答案 2 :(得分:0)

您可以使用FormArraythis article给出很好的总结。

还要确保您了解FormArrayFormGroup之间的differences