没有互联网时如何显示自定义错误消息

时间:2019-02-05 05:09:47

标签: javascript html angularjs

输入一些值后,当用户断开互联网连接并尝试执行任何操作时。我的应用程序未显示任何错误消息,说明“没有互联网连接” 在控制台中,它会抛出

angular_1.3.13.js:11613 TypeError:无法读取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)     完成时(angular_1.3.13.js:9665)     在completeRequest(angular_1.3.13.js:9855)

以下代码中的已处理错误。

this.handleError = function(状态,数据){

        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 :(得分:1)

您可以使用navigator.onLine来为在线和离线返回布尔值true / false

您还可以将online/offline事件附加到窗口

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

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