0

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.

  • Possible duplicate https://stackoverflow.com/questions/16242389/how-to-check-internet-connection-in-angularjs – tbone849 Jul 27 '19 at 10:31
  • See [AngularJS $http Service API Reference - Interceptors](https://docs.angularjs.org/api/ng/service/$http#interceptors) – georgeawg Jul 27 '19 at 13:23
  • The `ng-view` directive needs a router. Which router are you using? ngRoute or UI-router? – georgeawg Jul 27 '19 at 13:31
  • @georgeawg I'm using ngRoute. – ashima panjwani Jul 27 '19 at 13:43
  • It unusual for an app to be choosing between templates using `ng-include` and `ng-if`. That role is best done with a router. If the app needs nested views, consider using UI-Router. – georgeawg Jul 27 '19 at 15:10
  • @georgeawg I'm kind of new to angular so don't know much about the "usual way" of doing things. Thanks for the guidance. I'll look into UI-Router. – ashima panjwani Jul 27 '19 at 16:09

1 Answers1

0

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);
            }
        };
    }
})();
georgeawg
  • 48,608
  • 13
  • 72
  • 95
nima amr
  • 603
  • 1
  • 6
  • 13