2017-02-15 27 views
0

我已經在平均堆棧中開發了一個應用程序,並且還有一些進程需要額外的時間來執行,例如將數千個數據插入到mongodb中。如何在節點應用程序崩潰時在前端顯示某些東西(Angular)

它有時會崩潰,應用程序的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 + '% '); 
    }); 

回答

3

我推薦使用$http​​。攔截器將幫助您以通用的方式處理錯誤,因此您不必爲每個請求單獨使用錯誤回調。例如,你可以使用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'); 
+0

一個小的代碼示例將是巨大的 – Shaharyar

+0

上面的代碼實際上是ng-file select,所以它使用它的promise對象。但我知道你的攔截器的想法。謝謝。 – sarabs3

相關問題