简体   繁体   中英

problem with Display error messages in angular js

I need to display error message, when service issue.

i use below function to load data to the dropdown. function is loading in page load.

 component
loadOrgNames(){
      this.orgNameModel = this.dataserviceService.getOrgName();
     }

service

  getOrgName() : Observable<any> {
    return this.http.get(this.orgnameurl);

  }

how i handle errors in my service class.

the function getOrgName return an Observable, if the value is correct for dropdown use async pipe on your html. or use it like below:

this.dataserviceService.getOrgName().pipe(
tap(data => {
this.orgNameModel = data;

}))

This is how a service that returns Observable should be consumed. Error should be handled like below:

loadOrgNames(){
    this.dataserviceService.getOrgName().subscribe( response => {
      this.orgNameModel = reponse;
    },
    errorResponse => {
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM