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>`,
})
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 |