【发布时间】:2018-07-13 20:58:59
【问题描述】:
我需要在加载时在屏幕字段中显示从FireStore返回的数据,但是在订阅之前调用了buildForm(),所以没有显示屏幕上的字段中从 FireStore 返回的数据。
perfil-model.ts:
export class Perfil {
nome: string;
}
authService.ts:
getPerfil() {
return this.afs.doc<Perfil>(`perfil/${this.id}`)
.snapshotChanges()
.map(p => {
return { ...p.payload.data() };
});
}
perfilUser.component.ts:
perfil: Perfil = new Perfil();
perfilForm: FormGroup;
ngOnInit() {
const subscribe = this.authService
.getPerfil()
.subscribe((p: any) => {
subscribe.unsubscribe();
this.perfil = p;
})
this.buildForm();
}
buildForm() {
this.perfilForm = this.formBuilder.group({
nome: this.perfil.nome,
});
}
perfilUser.component.html:
<form [formGroup]="perfilForm" novalidade>
<div class="row">
<div class="form-group col-12 col-sm-12 col-md-7 col-lg-4 col-xl-4">
<label for="name">Nome*</label>
<input type="text" class="form-control" formControlName="nome" placeholder="Nome">
</div>
</form>
我检查并正在返回 FireStore 值,但我无法返回屏幕。
【问题讨论】:
-
使用默认值初始化
constructor中的表单,然后在订阅可观察时使用this.perfilForm.patchValue()更改表单字段 -
@Hareesh 为什么我们需要在构造函数中?把它放在外面应该也可以。如果是构造函数,为什么不使用 ngOnInit?
标签: angular typescript firebase google-cloud-firestore angularfire2