Angular2:使用模型驱动的表单添加和提交新表单组名称和值 [英] Angular2: add and submit a new formGroup name and values with model driven form

查看:105
本文介绍了Angular2:使用模型驱动的表单添加和提交新表单组名称和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过输入字段添加到FormArray

要通过表单输入向现有数组添加值,我可以使用html中的(click)="addAddress()",其中component.ts中定义了addAddress,以更新AppComponent表单中的数组中的值:

To add values, via a form input, to an existing array I can use (click)="addAddress()" in the html where addAddress is defined in the component.ts to update values in an array in the form AppComponent:

ngOnInit() {
        this.myForm = this._fb.group({
            name: ['', [Validators.required, Validators.minLength(5)]],
            addresses: this._fb.array([
                this.initAddress(),
            ])
        });
    }

    initAddress() {
        return this._fb.group({
            street: ['', Validators.required],
            postcode: ['']
        });
    }

    addAddress() {
        const control = <FormArray>this.myForm.controls['addresses'];
        control.push(this.initAddress());
    }

在htmlFor中,每次单击添加"按钮时,ngFor都用于添加一组输入字段":

And back in the html ngFor is used to add a set of input fields each time the 'add' button is clicked":

<div formArrayName="addresses">
          <div *ngFor="let address of myForm.controls.addresses.controls; let i=index" >
              <span>Address {{i + 1}}</span>
            <div [formGroupName]="i">
                <input type="text" formControlName="street">
                <input type="text" formControlName="postcode">
            </div>
          </div>
        </div>

就像这里的完整示例一样: https://embed.plnkr.co/sUjE1ULYhfDHLNBw2sRv/1

Like the full working example here: https://embed.plnkr.co/sUjE1ULYhfDHLNBw2sRv/1

通过输入字段添加到FormGroup

我想了解如何通过表单输入添加一个全新的FormGroup. 因此,以上述示例为例...

I would like to understand how to add a completely new FormGroup via form input. So taking the case of the example above...

而不是添加到地址数组中:

Rather than adding to an array of addresses:

{
  "name": "",
  "addresses": [
    {
      "street": "Baker Street",
      "postcode": "w2"
    },
    {
      "street": "Bond Street",
      "postcode": "w1"
    }
  ]
}

每次添加地址时,都会创建一个新的FormGroup,用户可以在其中通过表单输入为每个FormGroupName添加FormGroupName.例如:

Each time an address is added a new FormGroup is created where the user adds the FormGroupName for each via form input. For example:

{  
   "name":"",
   "home":{  
      "street":"Baker Street",
      "postcode":"w2"
   },
   "work":{  
      "street":"Bond Street",
      "postcode":"w1"
   }
}

推荐答案

使用addControl方法可以像这样实现:

Using the addControl method this can be achieved like so:

app.component.html摘录:

app.component.html excerpt:

        <div class="margin-20">
            <a (click)="addGroup(newName.value)" style="cursor: default">Add Group +</a>
          </div>

app.component.ts摘录:

app.component.ts excerpt:

ngOnInit() {

        this.myForm = this._fb.group({    
        });
    }


 addGroup(newName:string) {

         let data = this._fb.group({
                     foo: ['what a load of foo'],
                     bar: ['see you at the bar']

                    })

        this.myForm.addControl(newName, data);
        document.getElementById('newName').value='';


    }

plunk 中的工作示例:

对于一个示例,其中为添加到新命名组中的每个formControl发布相应的输入字段,我创建了这个朋克

For an example where a corresponding input field is published for each formControl that is added in the new named group I created this plunk

这篇关于Angular2:使用模型驱动的表单添加和提交新表单组名称和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆