anivaishu
anivaishu

Reputation: 25

Observable subscription twice

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

Answers (1)

Ashraful Islam
Ashraful Islam

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

Related Questions