动态增加响应式表单

页面文件:



<form *ngFor="let form of list" [formGroup]="form">
<div class="form-group">
<label>姓名:</label>
<input style="color: #000;" type="text" formControlName="name">
</div>
<div class="form-group">
<label>年龄:</label>
<input style="color: #000;" type="text" formControlName="age">
</div>
</form>

<button type="button" class="btn btn-primary" (click)="addForm()">增加表单</button>
<button type="button" class="btn btn-primary" (click)="printAllForm()">打印所有表单信息</button>

ts 文件:

 
 
import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';

@Component({
selector: 'app-dynamic-add-reactive-form',
templateUrl: './dynamic-add-reactive-form.component.html',
styleUrls: ['./dynamic-add-reactive-form.component.css']
})
export class DynamicAddReactiveFormComponent implements OnInit {
private list: Array<FormGroup>;
private formNum = 0;
constructor(
private fb: FormBuilder
) {}
ngOnInit() {
this.list = [];
}
public addForm(): void {
// 动态创建响应式表单
let newform = this['inFlowManholelistForm' + this.formNum];
newform = this.fb.group({
name: [''],
age: ['']
});
this.formNum += 1;
this.list.push(newform);
}
public printAllForm(): void {
this.list.map((value, index) => {
console.log(value.value);
});
}
}


---------------------记一记--------------

转载于:https://www.cnblogs.com/hello-dummy/p/9406289.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值