0

Below is a method in my Angular 2 service which returns an "Observable" of Array of "Category" objects.

// Get all categories
GetCategories():Observable<Array<Category>> {       

      return this.http.get('./categories')
                 .map(data => data.json())
                 .catch(this.handleError);;                                        

}

As per my understanding , the array of "Category" objects returned by the above service method can be accssed from Component code in 2 ways

Style 1

     this.appService.GetCategories().subscribe({next: (data) => {
                                                                    this.appService.categories = data;
                                                                    this.categories = this.appService.categories
                                                                },
                                                error: (err) => {
                                                                    this.toaster.error(err);
                                                                }                
     }); 

Style 2

    this.categories$ = this.appService.GetCategories(); // returns an Observable<Array<Category>>

and access the data in template using "async" pipe.

Now my question is,in case of style 1, I was able to display an error alert in case of an error , but if I go with style 2 , how will I able to display a custom error alert.

refactor
  • 13,954
  • 24
  • 68
  • 103

0 Answers0