How to handle template errors (and other errors) in angular 2?

When a template error appears in angular 2, the entire application does not work.

Should it work not only the component that had the template that caused the error, not working, but the rest of the application?

How to handle errors so that the application does not stop responding when an error occurs?

+5
source share
2 answers

You can use custom ErrorHandler :

 class MyErrorHandler implements ErrorHandler { handleError(error) { // do something with the exception } } @NgModule({ providers: [{provide: ErrorHandler, useClass: MyErrorHandler}] }) class MyModule {} 
+4
source

Using? mark is the solution around this by binding the object / array property to the template.

 "user?.name?.firstname" 

Looking for a more consolidated solution where we can handle template binding errors when a huge data object comes from the API and adds? for each property in an unstable and long solution.

-2
source

Source: https://habr.com/ru/post/1265946/


All Articles