Angular 多次生成formgroup
我用angular2反应式来表示:Angular 多次生成formgroup,angular,typescript,angular5,Angular,Typescript,Angular5,我用angular2反应式来表示: this.generateForm = this._fb.group({ id: [''], name: [''] }); 我有一个按钮“add”,当我点击它时,我需要这个按钮,同样的表单会被重新生成,新的表单会出现在html中。我该怎么办 <form [formGroup]="generateForm"> <div> <label>ID</label> &l
this.generateForm = this._fb.group({
id: [''],
name: ['']
});
我有一个按钮“add”,当我点击它时,我需要这个按钮,同样的表单会被重新生成,新的表单会出现在html中。我该怎么办
<form [formGroup]="generateForm">
<div>
<label>ID</label>
<input type="text" formControlName="id">
</div>
<div>
<label>Name</label>
<input type="text" formControlName="name">
</div>
<button (click)="add()"> add </button>
</form>
身份证件
名字
加
您应该使用FormArray。
看到这个了吗
名字
名称是必需的(至少5个字符)。
地址{i+1}
添加其他地址+
提交
我的表格详情:-
myForm有效吗?:
{{myForm.valid | json}
表单值:
{{myForm.value | json}
<form [formGroup]="myForm" novalidate (ngSubmit)="save(myForm)">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" formControlName="name">
<small *ngIf="!myForm.controls.name.valid" class="text-danger">
Name is required (minimum 5 characters).
</small>
</div>
<!--addresses-->
<div formArrayName="addresses">
<div *ngFor="let address of myForm.controls.addresses.controls; let i=index" class="panel panel-default">
<div class="panel-heading">
<span>Address {{i + 1}}</span>
<span class="glyphicon glyphicon-remove pull-right" *ngIf="myForm.controls.addresses.controls.length > 1" (click)="removeAddress(i)"></span>
</div>
<div class="panel-body" [formGroupName]="i">
<address [group]="myForm.controls.addresses.controls[i]"></address>
</div>
</div>
</div>
<div class="margin-20">
<a (click)="addAddress()" style="cursor: default">
Add another address +
</a>
</div>
<div class="margin-20">
<button type="submit" class="btn btn-primary pull-right" [disabled]="!myForm.valid">Submit</button>
</div>
<div class="clearfix"></div>
<div class="margin-20">
<div>myForm details:-</div>
<pre>Is myForm valid?: <br>{{myForm.valid | json}}</pre>
<pre>form value: <br>{{myForm.value | json}}</pre>
</div>
</form>