I have been trying to find a better solution to show error messages when restangular fails to load data. Its simple to show an alert box, but I am using toastr throughout the site for messages. Injecting the toastr dependency to the app.config doesn't work.
What is the best way to show toastr alerts for badd http requests?
Here is my router:
configAppControllers.config(['$stateProvider', '$urlRouterProvider', 'RestangularProvider',
function ($stateProvider, $urlRouterProvider, RestangularProvider) {
RestangularProvider.setErrorInterceptor(function (response) {
if (response.status == 401) {
alert('Oops, looks like something went wrong here.\nPlease try your request again later.\n\nError Code: ' + response.status);
//toastr.error('There has been an error contacting the server, please try again' + response.status);
} else if (response.status == 400) {
alert('Oops, looks like something went wrong here.\nPlease try your request again later.\n\nError Code: ' + response.status);
} else {
alert("Response received with HTTP error code: " + response.status );
}
return false;
});
// default route
$urlRouterProvider.otherwise("/cas");
$urlRouterProvider.when('/programhome/:programId', '/programhome/:programId/home');
// ui router states for dashboard views
//$urlRouterProvider.when('/');
$stateProvider
.state('cas', {
url: "/cas",
views: {
rightheader: {
templateUrl: 'components/headers/headerViewRight.html'
},
content: {
templateUrl: 'components/dashboard/intros/welcome.html'
}
}
})
.state('app', {
url: "/app/:casId",
views: {
rightheader: {
templateUrl: 'components/headers/headerLogoViewRight.html'
},
content: {
templateUrl: 'components/dashboard/intros/application.html'
}
}
})
.state('org', {
url: "/org",
views: {
rightheader: {
templateUrl: 'components/headers/headerLogoViewRight.html'
},
content: {
templateUrl: 'components/dashboard/intros/organizations.html'
}
}
})
.state('programslist', {
url: "/programs/:orgId",
views: {
rightheader: {
templateUrl: 'components/headers/headerIconsOrgs.html',
controller: 'headerIconsOrgsController as headerOrgCtrl'
},
content: {
templateUrl: 'components/dashboard/programDetails/programs.html'
}
}
})
}]).run(['$rootScope', '$timeout', '$location', '$state', 'toastr','$anchorScroll',
function ($rootScope, $timeout, $location, $state, toastr, $anchorScroll) {
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
console.log('event was:', toState);
});
$rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams) {
console.log('event was:', event);
});
$rootScope.$on('$stateChangeSuccess', function() {
document.body.scrollTop = document.documentElement.scrollTop = 0;
});
$timeout(function () {
$('.sidebar').css({
'height': $('.main-page').css('height')
});
$('.mv-sidebar').css({
'height': $('.main-page').css('height')
});
}, 200);
}]);