【发布时间】:2019-04-10 14:55:15
【问题描述】:
我正在使用 AngularFire2,如果函数没有从某个集合返回任何文档,我基本上想要做的是显示一个 div,即零。如果你返回一个或更多的文档,这个 div 应该会消失。
我尝试了以下方法,但没有得到预期的结果:
service.ts
size: number;
contadorSize;
contadorEventosPropios() {
const user = firebase.auth().currentUser;
this.contadorSize = this.afs
.collection('eventos', ref => ref.where('autorId', '==', user.uid))
.get().subscribe(snap => {
this.size = snap.size;
console.log(this.size);
});
return this.contadorSize;
}
component.ts
size: boolean;
contadorSize: number;
constructor( public fs: FirebaseService, private afs: AngularFirestore ) {}
ngOnInit() {
this.contadorSize = this.fs.contadorEventosPropios();
if (this.contadorSize === 0 ) {
return this.size = true;
} else {
return this.size = false;
}
}
component.html
<div *ngIf="size">
...
</div>
【问题讨论】:
-
用 return 语句反转 this.size = true/false。喜欢:this.size = true;返回;
-
@JacopoSciampi 我试过了,但即使函数返回 0,div 仍然没有显示
标签: angular google-cloud-firestore angularfire2