Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2、ngModel和Forms:list不';不能正确显示_Angular_Angular2 Forms - Fatal编程技术网

Angular2、ngModel和Forms:list不';不能正确显示

Angular2、ngModel和Forms:list不';不能正确显示,angular,angular2-forms,Angular,Angular2 Forms,我已经使用ngModel在输入中显示了一个项目数组。我在带有基本控件(必需)的表单中使用ngFor指令。列表不会正确显示:它始终是显示的数组的最后一项。 如果我使用mustache语法来显示输入之外的数组,就可以了。如果我删除表单和控件,就可以了。 您可以在此处进行测试:。 代码如下: @Component({ selector: "my-app", providers: [], template: " <div> <form [form

我已经使用ngModel在输入中显示了一个项目数组。我在带有基本控件(必需)的表单中使用ngFor指令。列表不会正确显示:它始终是显示的数组的最后一项。 如果我使用mustache语法来显示输入之外的数组,就可以了。如果我删除表单和控件,就可以了。 您可以在此处进行测试:。 代码如下:

    @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 }
];
@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 }
];
@组件({
选择器:“我的应用程序”,
提供者:[],
模板:“
索引:{{i}}
标签:{{person.label}}
值:{{person.value}}
", 指令:[反应形式指令] }) 导出类应用程序{ 私人人员控制:FormGroup; 私人:个人[]; 构造函数(私有_formBuilder:formBuilder){ 这个人=人; this.personControl=this.\u formBuilder.group({ personLabel:新表单控件(“”, [ 需要验证器 ]), personValue:new FormControl(“”, [ 需要验证器 ]) }); } } 出口类人员{ id:编号; 标签:字符串; 值:数字; } 施工人员:人员[]=[ {id:1,标签:“个人1”,值:10}, {id:2,标签:“第二个人”,值:20}, {id:3,标签:“第三个人”,值:30} ];
我试图了解FormarAyname,但它似乎无法处理多个输入,并且您无法使用ngModel。 有人有主意吗

我使用angular 2.0.0-rc.4和forms 0.2.0

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>

索引:{{i}}
标签:{p.label}}
值:{p.value}}
我还没有
FormGroup
方面的经验,因此我不知道是否有办法动态地将新的
FormControls
推送到
FormGroup
personControl
)对象上,不断调整名称。如果没有,则建议不要使用“模型驱动”形式


普朗克:好的,谢谢你的回复。我不明白formControlName必须是唯一的

我创建了另一个plunk,并对formControlName进行了更正:

代码如下:

    @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 }
];
@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 }
];
@组件({
选择器:“我的应用程序”,
提供者:[],
模板:`
索引:{{i}}
标签:{{person.label}}
值:{{person.value}}
`, 指令:[反应形式指令] }) 导出类应用程序{ 私人人员控制:FormGroup; 私人:个人[]; 构造函数(私有_formBuilder:formBuilder){ 这个人=人; 设ctrls={}; 此.persons.forEach(((person:person)=>{ ctrls[`personLabel${person.id}`]=新表单控件(“”, [ 需要验证器 ]); ctrls[`personValue${person.id}`]=新表单控件(“”, [ 需要验证器 ]); }).约束(这个); this.personControl=this.\u formBuilder.group(CTRL); } } 出口类人员{ id:编号; 标签:字符串; 值:数字; } 施工人员:人员[]=[ {id:1,标签:'personone',值:10}, {id:2,标签:'Person Two',值:20}, {id:3,标签:'Person Three',值:30} ];