How to manage with Angular2 a form who hold an undefined number of field ?
In my case, I need to create a from where user can add and delete some block of fileds. It's like an address book where user can add one or ten address. And each address had a some fields like street, street number and so on.
My look like this :
let address = fb.group({
street: fb.control(null, Validators.required),
streetNumber fb.control(null, Validators.required)
});
this.userForm = fb.group({
name: fb.control(null, Validators.required),
firstName: fb.control(null, Validators.required),
address: fb.group({
1: address
})
});
I really don't know how to manage this in the template.
I've try to write some thing like that in the template, but obviously, it doesn't work...
<form [ngFormModel]="userForm">
<input type="text" ngControl="name" #name="ngForm"/>
<input type="text" ngControl="firstName" #firstName="ngForm"/>
<div *ngFor="#address of userForm.controls['address'].controls">
<input type="text" ngControl="street" #street="ngForm"/>
<input type="text" ngControl="streetNumber" #streetNumber="ngForm"/>
</div>
EDIT
I've made a Plunker for a better explanation http://plnkr.co/edit/ffYe1479WnxYOQrbxwLF?p=preview
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…