<mat-select [(ngModel)]='selected' >
<mat-option *ngFor="let language of languages" [value]="language" >
{{language}}
</mat-option >
</mat-select>
这是我的html
selected:string
constructor(private firestore: AngularFirestore,private globals:GlobalsService) {
console.log(this.globals.userUid);
this.firestore.collection('customers').doc(this.globals.userUid).get().subscribe(function(doc){
if(doc.exists){
console.log(doc.data()["language"]);
this.selected = doc.data()["language"]
}
})
for (let index = 0; index < avaibleLanguages.length; index++) {
const language:string = avaibleLanguages[index]
this.languages.push(language)
}}
在console.log中,我看到我从firebase收到了正确的语言,但是没有将其设置为默认选择。这里所有其他东西都起作用
答案 0 :(得分:0)
只需要将值放在局部变量中即可。
var local
this.firestore.collection('customers').doc(this.globals.userUid).get()
.subscribe(doc =>{
if(doc.exists){
local = doc.data()["language"]
}
},err => console.log(err),()=>{this.selected = local }
)
for (let index = 0; index < avaibleLanguages.length; index++) {
const language:string = avaibleLanguages[index]
this.languages.push(language)
}
答案 1 :(得分:0)
如果症状持续,请使用ChangrDetectorRef:在此处https://angular.io/api/core/ChangeDetectorRef
...
constructor (private changeDetector: ChangeDetectorRef) { }
private someFn(): void {
this.changeDetector.detach();
this.changeDetector.reattach();
this.changeDetector.detectChanges();
}
...