harryjohn
harryjohn

Reputation: 686

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

When ever there is a template error in angular 2, the entire application fails to work.

Shouldn't only the component that had the template that caused the error, fail to work and the rest of the application be working fine?

How to handle errors so that the application won't stop being responsive when an error occurs?

Upvotes: 7

Views: 4954

Answers (2)

Bhaskar Reddy
Bhaskar Reddy

Reputation: 79

Errors that comes while parsing the angular template or while evaluating the expression cannot be handled by angular Errorrhandller. These kind of errors needs to be fixed.

You can avoid the following mistakes to prevent the template error occurring: Don't bind methods in directives like *ngIf , *ngFor. Don't write big expression in directives which likely would cause error. Keep a minimal amount of logic in templates.

Anyone can add if anything missing.

Upvotes: 2

Sasxa
Sasxa

Reputation: 41314

You can use custom ErrorHandler:

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

Upvotes: 5

Related Questions