Angular2, ngModel and Forms: the list does not display correctly

I have an array of elements displayed on inputs using ngModel. I use the ngFor directive inside the form with basic control (required). The list does not display correctly: it is always the last element of the array that is displayed. If I use mustache syntax to display an array of external inputs, that's fine. If I delete the form and the control, this is normal. 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'm trying to peek into formArrayName, but it seems that it does not work with multiple inputs, and you cannot use ngModel. Does anyone have an idea?

I am using angular 2.0.0-rc.4 and it forms 0.2.0

+4
2

formControlName="personLabel" formControlName="personValue" . , persons .

FormControl :

this.personControl = new FormGroup({
  personLabel0 : new FormControl('', 
    [
      Validators.required
    ]),
    personValue0 : new FormControl('', 
    [
      Validators.required
    ]),
    personLabel1 : new FormControl('', 
    [
      Validators.required
    ]),
    personValue1 : new FormControl('', 
    [
      Validators.required
    ]),
    personLabel2 : new FormControl('', 
    [
      Validators.required
    ]),
    personValue2 : new FormControl('', 
    [
      Validators.required
    ])
});

formControlName :

public getName(word, i) {
    return "person" + word + i;
}

:

<div *ngFor="let p of persons; let i = index">
      index : {{i}}<br/>
      label : {{p.label}}<br/>
      value : {{p.value}}<br/>
      <input type="text" 
             maxlength="30" 
             [id]="'label-'+p.id" 
             [(ngModel)]="p.label"
             formControlName="{{getName('Label', i)}}"
             placeholder="{{p.id}}"/>
      <input type="text" 
             maxlength="30" 
             [id]="'value-'+p.id" 
             [(ngModel)]="p.value"
             formControlName="{{getName('Value', i)}}"/>
    </div>

FormGroup, , FormControls FormGroup (personControl), . , .

Plunker: https://plnkr.co/edit/ERWA6GKX9VYADouPb6Z2?p=preview

+1

, . , formControlName .

plunk formControlName:  https://plnkr.co/edit/chiCdN5A7Vb4MCrAYaSE?p=info

:

@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'+person.id" />
          <input type="text" 
                 maxlength="30" 
                 [id]="'value-'+person.id" 
                 [(ngModel)]="person.value"
                 [formControlName]="'personValue'+person.id" />
        </div>
      </form>
    </div>
  `,
  directives: [REACTIVE_FORM_DIRECTIVES]
})
export class App {
  private personControl: FormGroup;
  private persons : Person[];
  constructor(private _formBuilder: FormBuilder) {
    this.persons = PERSONS;
    let ctrls = {};

    this.persons.forEach(((person: Person) => {
      ctrls[`personLabel${person.id}`] = new FormControl('', 
        [
          Validators.required
        ]);
        ctrls[`personValue${person.id}`] = new FormControl('', 
        [
          Validators.required
        ]);
    }).bind(this));
    this.personControl = this._formBuilder.group(ctrls);

  }
}

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 }
];
0

Source: https://habr.com/ru/post/1649167/


All Articles