我在平均堆栈中开发了一个应用程序,有些过程需要额外的时间来执行,例如将数千条数据插入mongodb。
它有时会崩溃,并且应用程序的 UI 悬在中间。
有什么方法可以克服这个问题,并在应用程序崩溃时向用户显示一些错误消息。
下面是一些代码示例:-
Upload.upload({
url: '/api/uploadarchive',
data: {file: file}
}).then(function (resp) {
$scope.progress = false;
console.log('Success ');
}, function (resp) {
console.log('Error status: ' + resp.status);
}, function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
//console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
console.log('progress: ' + progressPercentage + '% ');
});
最佳答案
我建议使用 $http
interceptors 。拦截器将帮助您以通用方式处理错误,因此您不必单独为每个请求使用错误回调。例如,您可以使用 responseError
拦截器检查任何请求的响应状态是否为 404
,在这种情况下重定向到错误页面:
使用拦截器定义工厂:
yourAppModule.factory('myHttpInterceptor', function($q, $state, dependency2) {
return {
'responseError': function(rejection) {
var status = rejection.status;
if (status === 404) {
$state.go('errorPage');
}
return $q.reject(rejection);
}
};
});
然后在您的应用程序config
中注册它:
$httpProvider.interceptors.push('myHttpInterceptor');
关于angularjs - 当 Node 应用程序崩溃时如何在前端(Angular)上显示某些内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42243544/