为什么我必须在FirebaseObjectObservable上使用$ value

时间:2017-05-22 18:07:41

标签: angular ionic2 angularfire2

我的Firebase DB中有以下节点:

users
-$user
--public
---avatarUrl: 'https://myurl'

我像这样检索这个网址:

lastMessage$: Observable<any>;
  recipientAvatarUrl$: FirebaseObjectObservable<any>;

  constructor(
    public navCtrl: NavController,
    public messageService: MessageService,
    public authService: AuthService,
    public userService: UserService
  ) {
    console.log('ChatPreviewComponent#constructor');
  }

  ngOnInit(): void {
    this.recipientAvatarUrl$ = this.userService.getUserAvatarUrl(this.chat.recipient, this.authService.getCurrenUserCompany());
  }

功能&#34; getUserAvatarUrl&#34;在我的服务中如下:

getUserAvatarUrl(uid: string, company: string): any {
    console.log('UserService#getUserAvatarUrl - Get user avatar url for ', uid);
    let path = `/${company}/users/${uid}/public/avatarUrl`;
    return this.af.database.object(path);
  }

最后,我的视图是渲染图像的地方:

<ion-item (click)="openChat(shortChat.id)">
  <ion-avatar item-left>
    <img [src]="(recipientAvatarUrl$ | async)?.$value" />
  </ion-avatar>
</ion-item>

我不明白为什么我必须使用$ value来获取节点的值。我虽然异步管道会根据AngularFire2文档解包FirebaseObjectObservable。

有任何线索吗?

1 个答案:

答案 0 :(得分:1)

如果您要订阅的FirebaseObjectObservable节点包含一个对象,则可以像在JavaScript对象中一样访问字段。

<img [src]="(recipientAvatarUrl$ | async)?.name" />

原始值(数字,字符串或布尔值)存储在$ value下。

<img [src]="(recipientAvatarUrl$ | async)?.$value" />