且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular2:使用模型驱动的表单添加和提交新表单组名称和值

更新时间:2023-10-25 23:14:34

使用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