在您的组件中
obj = {
id: '',
name: ''
}
constructor(private fb: FormBuilder) {
this.demoForm = this.fb.group({
fields: []
});
}
// You can look for the value changes of select, which you can subscribe to
this.demoForm.valueChanges.subscribe(
id => {
if(id) {
this.countries.forEach(value => {
if(value.id === id) {
this.obj = value;
}
})
} else {
this.obj = null; // or assign for empty object
}
在你看来
<div class="form-group">
<form [formGroup]="demoForm">
<select formControlName="fields"
name="fields"
class="form-control">
<option *ngFor="let field of fields"
[value]="field.id">{{ field.name }}</option>
</select>
</div>
<p>{{obj | json}}</p>
希望这能帮上忙,让你有个想法。。。