Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show message if Angular app failed to load / crashed?

Tags:

angularjs

So I want to handle a scenario where my Angular app failed to load or crashed due to an error and in those cases like a 'noscript tag' I want to display something helpful.

The primary reason for this is I noticed some of my angular modules may get blocked by an AdBlocker so the js file that instantiates that module does not get loaded and the App gives a 'nomodule error'.

Are there any best practices or ways to handle such situations so I could at least display a message telling my users a meaningful message?

As right now when this happens I just get a blank white page... not good.

like image 474
iQ. Avatar asked Feb 07 '26 03:02

iQ.


1 Answers

So after trying several things, I found this as a way to handle such errors that involve angular failing to initialise:

// init the app manually and catch any errors
try {
    angular.bootstrap(document, [myApp]);
} catch(e) {
    window.location.href = '/app-load-error'
}
like image 173
iQ. Avatar answered Feb 12 '26 05:02

iQ.