angular2表单,<select>元素中的选定项

时间:2016-09-16 16:15:48

标签: angular typescript

这是FormBuilder: let valuesArray = fb.array([         fb.group({name:'one'}),         fb.group({name:'two'}),         fb.group({name:'three'}),         fb.group({name:'four'})     ]); this.form = fb.group({     values:valuesArray,     selectedValue:valuesArray [0] }) 模板: &lt; select formControlName =“selectedValue”&gt;    &lt; option * ngFor =“let formGroup of form.controls.values.controls”&gt;      {{controlGroup.controls.name.value}}    &LT; /选项&GT; &LT; /选择&GT; 抛出错误control.registerOnChange不是通过从&lt; select&gt;中删除formControlName来修复的函数。元素,但你怎么跟踪列表中的选定项目?

1 个答案:

答案 0 :(得分:1)

我认为这应该做你想要的:

<div>{{selectedItem | json}}</div>
<select formControlName="mySelect" [(ngModel)]="selectedItem">
  <option *ngFor="let item of items" [(ngValue)]="item">
this.form = fb.group({
    mySelect: []
})

items = [
    { name: 'one' },
    { name: 'two' },
    { name: 'three' },
    { name: 'four' }];

selectedItem; 
// or
selectedItem = items[0];