rxjs - Angular 2 How to detect string change using observable -


i have 2 services. in 1 want subscribe detect if string changes in other service load database each time changes.

first service:( session value used reference 2nd service)

subscribestringchange() {     this.session.uid.subscribe(x => {         console.log(this.session.uid);         return this.af.database.list('items', {             query: {                 orderbychild: 'uid',                 equalto: this.session.uid             }         });     }); } 

here second service , have tried:

uid: observable<string> = observable.of('');  constructor(private auth: firebaseauth, private af: angularfire) {     this.auth.subscribe(user => {         if (user) {            this.uid = observable.of(user.uid);             console.info('logged in');         } else {              this.uid = observable.of('');             console.info('logged out');         }     }); } 

you use behaviorsubject, acts both producer , consumer (or more precisely, observer , observable). on side use firebase, acting on producer side, emitting new value, , on other side, keep same

import { behaviorsubject } 'rxjs/behaviorsubject';  private _uid = new behaviorsubject<string>('');  uid = this._uid.asobservable();  constructor(private auth: firebaseauth, private af: angularfire) {     this.auth.subscribe(user => {         if (user) {            this._uid.next(user.uid);             console.info('logged in');         } else {              this.uid.next('');             console.info('logged out');         }     }); } 

notice this._uid.next(''). here emitting new value, , subscriber(s) receive it.

you don't need change in other service. do. trying access uid, observable, when should use value passed subscribe callback, x

this.session.uid.subscribe(x => {     console.log(x);     return this.af.database.list('items', {         query: {             orderbychild: 'uid',             equalto: x         }     }); }); 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -