【发布时间】:2018-08-09 03:33:45
【问题描述】:
我正在使用 Angular 和 Angularfire2。
我想检查 Firestore 集合是否为空。我正在制作一个待办事项应用程序,我想显示一个空状态容器作为Material guidelines 的一部分。
这是我的数据结构:
users/
userID/
todos/
// etc.
我正在使用以下代码:
<div class="empty-state-container" *ngIf="isTodosEmpty">
<div class="empty-state-div">
<h2>All todos have been done!</h2>
<p>Go reward yourself with exercising, reading or your favourite hobby.</p>
<button mat-raised-button (click)="newTodo()" color="primary">Create a new todo</button>
</div>
</div>
<!-- ... -->
我的组件:
export class MyComponent {
isTodosEmpty: boolean;
constructor(
private fs: AngularFirestore,
private afAuth: AngularFireAuth,
// ...
) {
afAuth.auth.onAuthStateChanged((user) => {
if (user) {
console.log(user);
this.currentUser = user.uid;
this.todosCollection = this.fs.collection(`users/${this.currentUser}/todos`);
this.todos$ = this.todosCollection.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as TodoItem;
const id = a.payload.doc.id;
return { id, ...data };
});
});
} else {
console.warn('Current user doesn\'t exist yet!');
}
});
}
}
【问题讨论】:
标签: angular firebase angular-material google-cloud-firestore