在Angular 5 + Firebase(AngularFire)项目中,我有服务Current
,我从Firebase RDB获取当前用户数据:
export class CurrentService {
public current;
constructor(
private authFb: AngularFireAuth,
private db: AngularFireDatabase
) {
this.isLoggedIn().pipe(
tap(user => {
if (user) {
this.current = this.db.object(`users/${user.uid}`)
.valueChanges()
} else {
this.current = null;
console.log("no")
}
})
)
.subscribe()
}
isLoggedIn() {
return this.authFb.authState.pipe(first())
}
}
在视图中我想要绑定current
值:
{{ currService.current?.username }}
但它的回归null
。有什么奇怪的,因为当我用{j}格式的字段和值绑定{{ (currService.current | async) | json }}
它的返回完整对象时。我做错了什么?
答案 0 :(得分:1)
您还必须使用async
管道:
{{ (currService.current | async)?.username }}