I have to get datas from two subscribe but I get always data of the first one.
I have a data shared service :
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class DataService {
private source = new BehaviorSubject<any>('');
data = this.source.asObservable();
constructor() { }
update(values: any) {
this.source.next(values);
}
}
Before leaving search component, I call update method.
Now, I'm on the results component. I get shared data like that :
constructor(private dataSvc: DataService,
private router: Router,
private rideStore: RideStore) { }
ngOnInit() {
this.getData();
}
getData() {
this.subscription = this.dataSvc.data.take(1).subscribe(
data => this.data = data ? data : undefined,
err => console.log(err),
() => this._isValid()
);
}
My question is : I need shared data to subscribe to another observable. First, I construct an object ride and after i call the search method
search() {
this.rideStore.searchRides(this.ride).subscribe(
rides => {
// this.dataSvc.update(rides);
this.rides = rides;
console.log('results', this.ride);
},
err => console.log('err', err)
);
}
The problem is I always get data from data service and not from the api call. The api works cause I intercept results in store but not in the component. So How I can stop subscribing first observable and subscribe of the second ?
this.data = data ? data : undefined
You surely meanthis.data === data ? data : undefined
– Jeremy Thillethis.data = (data ? data : undefined)
– Jeremy Thillethis.sub1
andthis.sub2
and just have two subs. – Jeremy Thille