Reputation: 25
I am using angular 4 with observable.
Actually shared data between components using subject and observables.
Here is my function.
getNewSearchResults(): Observable<any> {
return this._searchResultOne.asObservable();
}
I just subscribe the function using this
this.searchService.getNewSearchResults().subscribe(res=>{
console.log("res",res);
})
same time result is returned twice.why?
How can i do it.
Please advice me,
thanks
Upvotes: 1
Views: 570
Reputation: 1263
Try to do the subscription in ngOnInit()
method and unsubscribe the observable in ngOnDestroy()
method. I hope this will solve your problem.
Upvotes: 1