我有angular2反应形式,其中包含md-select以使用户从firebase中选择医生md-select在渲染组件之前数据存在时工作正常,但如果数据像asservable一样异步,则不显示选定值
doctors$:Observable< Doctor[] > = this.doctorService.findAllDoctors();
constructor(private fb:FormBuilder , public doctorService : DoctorService
,private cdr: ChangeDetectorRef )
{
this.doctors$= this.doctorService.findAllDoctors();
this.form = this.fb.group({
FullName: ['',Validators.required],
Ext: [''],
BirthDate: ['',Validators.required],
Gender: ['',Validators.required],
Phone: ['',[Validators.required]],
doctorId: ['']
});
<md-select formControlName="doctorId" ngControl="doctorId" placeholder="Doctor" >
<md-option *ngFor="let p of doctors$ | async " [value]="p.value">{{p.value}}</md-option>
</md-select>
在这里输入代码
答案 0 :(得分:0)
解决方法在异步加载选项数据后用选定的值更新md-select
ngOnInit() {
this.doctors$.subscribe(c=>{
this.form.controls['doctorId'].patchValue(this.initialValue.doctorId);
});