Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Angular 5 subscribe and unsubscrive Observable

florianzobele:

I have to get datas from to 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('');
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 ?



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Angular 5 subscribe and unsubscrive Observable

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×