Search This Blog

Wednesday 16 August 2017

Catching exceptions and displaying error messages in view using Angular2

We can display cached error message in view from component for that need write the following code
in Appcomponent

export class AppComponent {
    iproducts: IProduct[];
    err: any;
    constructor(private _product: ProductService) {
    }
    ngOnInit(): void {
      this._product.getproducts()
          .subscribe(iproducts => this.iproducts = iproducts,
          error => this.err = error);
    }
}


and in HTML Page or template we need to write  following code. Focus only on bold code rest of the code responsible for get values from service

@Component({
    selector: 'my-app',
    template: `<h1>{{err}}</h1>`,
  })

Catching exceptions and displaying error messages in view using Angular2

No comments:

Post a Comment