What is the best practice to unsubscribe within a Angular2 service from a http subscription?
Currently I do this but I'm not sure if this will be the best way.
import { Injectable } from "@angular/core";
import { Http } from "@angular/http";
import { Subject } from "rxjs/Subject";
import { ISubscription } from "rxjs/Subscription";
@Injectable()
export class SearchService {
private _searchSource = new Subject<any>();
public search$ = this._searchSource.asObservable();
constructor(private _http: Http) {}
public search(value: string) {
let sub: ISubscription = this._http.get("/api/search?value=" + value)
.map(response => <any>response.json())
.do(data => this._searchSource.next(data))
.finally(() => sub.unsubscribe()).subscribe();
}
}