0

我有 angular2 反应形式,其中包含 md-select 以使用户从 firebase 中选择医生 md-select 在呈现组件之前存在数据时可以正常工作,但是如果数据以可观察的方式异步传入,则它不会显示所选值

 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>

在这里输入代码

4

1 回答 1

0

通过在异步加载选项数据后使用选定值更新 md-select 解决

 ngOnInit() {
   
this.doctors$.subscribe(c=>{

    this.form.controls['doctorId'].patchValue(this.initialValue.doctorId);


});

于 2017-02-06T01:50:34.963 回答