How do I catch the internet disconnected error when loading different views using ng-include

385 views Asked by At

Inside my ng-view, I have included different views using ng-include. I'm using ng-if to show or hide any view based on the URL parameters.

Inside my ng-view

<div>
    header
</div>
<ng-include src="'../views/about.html'" ng-if="obj.var1"></ng-include>
<ng-include src="'../views/contact.html'" ng-if="obj.var2"></ng-include>
<div>
    footer
</div>

Inside my controller

app.controller('myCtrl', ['$scope', '$location', function($scope, $location){
    if($location.search().hasOwnProperty('about')){
        $scope.obj.var1 = true;
    }
}])

The problem is occuring when my page is not connected to the internet. I'm getting an internet disconnected error and my HTML inside the ng-include is not getting loaded.

I would like to know how to catch this error, so that I can print out an appropriate message on the screen. I have tried a simple try and catch, but it didn't seem to work.

1

There are 1 answers

0
nima amr On

I did not exactly figure out what you want to do. but you need get status internet online or internet offline can get help this code :

(function () {
    "use strict";
    angular
        .module("YourApp.Common")
        .factory("httpRequestInterceptor", HttpRequestInterceptor);


    HttpRequestInterceptor.$inject = ["$q"];
    function HttpRequestInterceptor($q) {
        return {
            responseError: function (rejection) {
                if (rejection.status === 0) {
                    // For example - redirect user to an "offline" page here
                }

                return $q.reject(rejection);
            }
        };
    }
})();