This may not be exactly what you want, but it may help:
Suppose you have something like this (taken from the Hero Guide on the Angular 2 website):
(Typescript)
@Injectable()
export class HeroService {
private heroesUrl = 'api/heroes';
constructor (private http: Http) {}
getHeroes(): Observable<Hero[]> {
return this.http.get(this.heroesUrl)
.map(this.extractData);
}
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}
}
let subscription = service.getHeroes().subscribe( ... do stuff here );
subscription.unsubscribe();
- , Observable, Angular 2, :
getHeroes(): Observable<Hero[]> {
return Observable.create(
(observer: any) => {
const subscription = this.http.get(this.heroesUrl)
.map(this.extractData).subscribe(observer);
return () => {
subscription.unsubscribe();
this.myTearDownLogic();
}
}
);