I've got an array of items displayed in inputs using ngModel. I use an ngFor directive inside a form with a basic control (required). The list doesn't display corrrectly : it's always the last item of the array which is displayed. If i use mustache syntax to display the array outside inputs, it's ok. If i remove the form and the control it's ok. You can test it here : plunker. Here is the code :
@Component({
selector: "my-app",
providers: [],
template: "
<div>
<form [formGroup]="personControl">
<div *ngFor="let person of persons; let i = index">
index : {{i}}<br/>
label : {{person.label}}<br/>
value : {{person.value}}<br/>
<input type="text"
maxlength="30"
[id]="'label-'+person.id"
[(ngModel)]="person.label"
formControlName="personLabel"/>
<input type="text"
maxlength="30"
[id]="'value-'+person.id"
[(ngModel)]="person.value"
formControlName="personValue"/>
</div>
</form>
</div>
",
directives: [REACTIVE_FORM_DIRECTIVES]
})
export class App {
private personControl: FormGroup;
private persons : Person[];
constructor(private _formBuilder: FormBuilder) {
this.persons = PERSONS;
this.personControl = this._formBuilder.group({
personLabel : new FormControl("",
[
Validators.required
]),
personValue : new FormControl("",
[
Validators.required
])
});
}
}
export class Person {
id: number;
label: string;
value : number;
}
const PERSONS: Person[] = [
{ id: 1, label: "Person One", value : 10 },
{ id: 2, label: "Person Two", value : 20 },
{ id: 3, label: "Person Three", value : 30 }
];
I try to have a look into formArrayName but it seems that it doesn't work with several inputs and you can't use ngModel. Somebody has an idea?
I use angular 2.0.0-rc.4 and forms 0.2.0