Как показать пользовательское сообщение об ошибке, когда нет интернета - PullRequest
0 голосов
/ 05 февраля 2019

После ввода некоторых значений, когда пользователь отключает интернет и пытается выполнить какое-либо действие.В моем приложении не отображается сообщение об ошибке «Нет подключения к Интернету». В консоли выдается

angular_1.3.13.js: 11613 TypeError: Невозможно прочитать свойство 'error' из null в Object.handleError (apiHelper.js: 120) в shortnoteCreateController.js: 88 в angular_1.3.13.js: 9382 в processQueue (angular_1.3.13.js: 13195) в angular_1.3.13.js: 13211 в Scope. $ eval (angular_1.3.13.js:14407) в Scope. $ Digest (angular_1.3.13.js: 14223) в Scope. $ Apply (angular_1.3.13.js: 14512) в done (angular_1.3.13.js: 9665) в completeRequest (angular_1.3.13.js:9855)

Обрабатывается ошибка в приведенном ниже коде.

this.handleError = function (status, data) {

        if (status == URLs.unauthorizedCode && address.location.hash != "#/login") {
            var userdet = this.getUserDetailsFromServer(URLs.HTTP_GET, URLs.requestheaders);
            userdet.success(function(data) {
                //navigate to login page when unauthorized operations performed
                //its a rare case of getting permission reverted by PA when in action
                //or if logged out in another tab or logged in as another user in another tab
                //localStorageService.set('successmsg', 'You have been prevented from an unauthorized access');
                 displayResult.showErrorResults(Constants.authorizationFailureMsg,'success');
                address.location.href = URLs.loginView;
                //address.location.reload();
                //console.info("in handle error auth fail");

            });
            userdet.error(function(data) {
                localStorageService.set('successmsg', 'You have been prevented from an unauthorized access');

                localStorageService.clearAll();
                address.location.href = URLs.loginView;
                address.location.reload();
                displayResult.showErrorResults(Constants.authorizationFailureMsg,'success');
                //console.info("in handle error auth fail");

            });
        } else if(status == URLs.notFoundCode){
            address.location.href = URLs.notFoundPage;
        }else {

            $rootScope.rs_error_status = status;
            //console.log("inside apihelper", data);
            if (data.error) {
                $rootScope.rs_error_data = data;
                //console.info("in error");
            } else if (data.errors) {
                //console.info("in errors");
                $rootScope.rs_error_data = data.errors;
            }
            var modalInstance = $modal.open({
                templateUrl : 'userinterface/pages/modal/errordialog.html',
                controller : 'modalController'
            });

            modalInstance.result.then(function() {
                //console.info("Exiting modal popup");
            }, function() {
            });

        }

1 Ответ

0 голосов
/ 05 февраля 2019

Вы можете использовать navigator.onLine , который возвращает логическое значение true / false для онлайн и офлайн

Также вы можете прикрепить online/offline событие к окну

window.addEventListener('offline', (e)=> { //code here});

window.addEventListener('online', (e) => { //some code here});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...