2017-06-01 16 views
0

我试图使用FormArray完成动态复制,但我无法做到。请检查JSFiddle上的表格。无法使用formArray动态复制控件

component.html

<div class="form-group"> 
    <div formArrayName="nqCoordinators"> 
    <button class="col-md-offset-1 col-md-1 btn btn-default" type="button"> 
     Add Coordinator 
    </button> 
    <div class="form-group" [ngClass]="{'has-error': (agencyForm.get('nqCoordinators').touched || agencyForm.get('nqCoordinators').dirty) && !agencyForm.get('nqCoordinators').valid}" 
     *ngFor="let nqCoordinator of nqCoordinators.controls; let i=index"> 
     <label class="col-md-2 control-label" [attr.for]="i">Coordinator</label> 
     <div class="col-md-8"> 
     <input class="form-control" id="i" type="text" placeholder="NQ Coordinator" formControlName="i" /> 
     </div>    
    </div> 
    </div> 
</div> 

component.ts

import { 
    Component, 
    OnInit 
} 
from '@angular/core'; 
import { 
    FormGroup, 
    FormControl, 
    FormBuilder, 
    Validators, 
    FormArray 
} 
from '@angular/forms'; 
import { 
    ActivatedRoute, 
    Router 
} 
from '@angular/router'; 
import { 
    Subscription 
} 
from 'rxjs/Subscription'; 


@Component({ 
    templateUrl: './newq.component.html' 
}) 
export class CreateNewQComponent implements OnInit { 
    pageTitle: string; 

    agencyForm: FormGroup; 


    private sub: Subscription; 
    get nqCoordinators(): FormArray { 
    return <FormArray>this.agencyForm.get('nqCoordinators'); 
    } 

    constructor(private route: ActivatedRoute, private fb: FormBuilder, private router: Router) {} 

    //myOptions: IMultiSelectOption[] = [ 
    // { id: 1, name: 'Option 1' }, 
    // { id: 2, name: 'Option 2' }, 
    //]; 

    ngOnInit(): void { 
     this.agencyForm = this.fb.group({ 
     nqCoordinators: this.fb.array([]), 
     //qAdvisors: '', 
     nq1: '', 
     nq2: '', 
     nq3: '', 
     nq4: '', 
     nqCoordinators: this.fb.array([]), 
     nqStartDate: ''   
     }); 

     this.agencyForm.controls['salesRep'].valueChanges 
      .subscribe((selectedOptions) => { 
       // changes 
      }); 
     this.sub = this.route.params.subscribe(
     params => { 
      let id = +params['id']; 
      if (id === 0) 
      this.pageTitle = 'Add Q'; 
      else 
      this.pageTitle = 'Edit Q'; 
     } 
    ); 

    } 

    add() { 
    console.log(this.newQForm); 
    console.log('Saved: ' + JSON.stringify(this.newQForm)); 
    } 

    addAgency() { 
    console.log(this.newQForm); 
    console.log('Saved: ' + JSON.stringify(this.newQForm)); 
    } 

    backBtnClick() { 
    this.router.navigate(['/newq']); 
    } 
} 
+0

你是什么意思*重复控件*?究竟是什么问题,目前尚不清楚?目前的行为是什么?预期的行为是什么? – Alex

+0

我试图动态复制协调员的文本框字段(如果你能够看到在JSFiddle),它不会工作。 – user1704842

+0

我想你的意思是在数组中添加更多的协调器?我猜想是这样的,不得不确定:)我会看看你的代码,看看我们是否可以找出这个问题。 – Alex

回答

1

你有很多的代码存在,所以这里是一个简单的例子。

首先,你有一个嵌套的<form>标签。这不仅仅是工作。相反,您应该在模板中只有一个主要的[formGroup](带有表单标签)。其余的模板应该是formControlName的。 FormArrayName和嵌套FormGroupName's。

所以这里会形式,在那里我们有一个协调最初设定的构建:

this.agencyForm = this.fb.group({ 
    agencyName: [''], 
    nqCoordinators: this.fb.array([ 
    this.initNqCoordinators() // we'll use the same function for adding new 
    ]) 
}); 

initNqCoordinators() { 
    return this.fb.group({ 
    // here add all your form controls needed for your coordinator 
    whatever_form_control: [''] 
    }) 
} 

,在这里,当你模板单击添加一个新的协调,它是这样的:

addCoordinator() { 
    // our formarray with coordinators 
    const control = <FormArray>this.agencyForm.controls['nqCoordinators']; 
    // we call function to push a new formgroup to the array 
    control.push(this.initNqCoordinators()) 
} 

然后你的模板应该是这样的:

<form [formGroup]="agencyForm" (ngSubmit)="submit(agencyForm.value)"> 
    <label>Agency Name: </label> 
    <input formControlName="agencyName"/><br><br> 
    <button (click)="addCoordinator()">Add Coordinator</button><br> 

    <div formArrayName="nqCoordinators"> 
    <div *ngFor="let child of agencyForm.controls.nqCoordinators.controls; let i = index" > 
     <div formGroupName="{{i}}"> 
      <label>Whatever Coordinator formControl-name</label> 
      <input formControlName="whatever_form_control" /><br><br> 
     </div> 
    </div> 
    </div> 
    <button type="submit">Submit</button> 
</form> 

A DEMO同上。

+0

非常感谢回复的人!你让它看起来更容易。我感谢您的帮助!! – user1704842

+0

没问题!我知道它一开始可能会让人困惑。在我围绕着反应形式的工作方式缠绕头之前,我搔了一会儿头:)很高兴听到它有帮助。 – Alex