Angular 2 md-select 从 Observable 数组填充时不绑定 selected 值
Angular 2 md-select don't bind selected value when populate from Observable array
我有包含 md-select 的 angular2 反应形式,使来自 firebase 的用户 select doctor md-select 在呈现组件之前存在数据时工作正常,但如果数据到来async as observable 它不显示 selected 值
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>
在此处输入代码
通过在异步加载选项数据后用选定值更新 md-select 解决
ngOnInit() {
this.doctors$.subscribe(c=>{
this.form.controls['doctorId'].patchValue(this.initialValue.doctorId);
});
我有包含 md-select 的 angular2 反应形式,使来自 firebase 的用户 select doctor md-select 在呈现组件之前存在数据时工作正常,但如果数据到来async as observable 它不显示 selected 值
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>
在此处输入代码
通过在异步加载选项数据后用选定值更新 md-select 解决
ngOnInit() {
this.doctors$.subscribe(c=>{
this.form.controls['doctorId'].patchValue(this.initialValue.doctorId);
});