代码之家  ›  专栏  ›  技术社区  ›  Jus10

如何从AngularFire2中的行为主体中获取当前值?

  •  1
  • Jus10  · 技术社区  · 7 年前

    我正在使用AngularFireDatabase来获取我的部分数据库,就像这样 db.object('/accounts/'+this.cid); 哪里 this.cid BehaviorSubject 这是可以改变的。我是不是走错了路?

    cid: BehaviorSubject<string>;
    account: FirebaseObjectObservable<any>;
    constructor(private db: AngularFireDatabase) {
      this.cid = new BehaviorSubject<string>('fs2ejD4ds');
      this.account = db.object('/accounts/'+this.cid);
    

    基本上每次我跑步的时候 this.cid.next('fhEj2jd') 我希望它能改变我们在Firebase中引用的对象。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Pengyy    7 年前

    用于从中获取当前值 BehaviorSubject getValue docs .

    this.account = db.object('/accounts/'+this.cid.getValue());
    

    但是请记住,您必须覆盖原始的可观察值 this.account 每次 this.cid 这账户 ,则您必须先取消订阅,然后再使用新的observable覆盖它(对于取消订阅,您可以使用异步管道自动完成)。

    demo .