@Calendee@mhartington I am trying to check internet connectivity in my ionic cordova program in android and I am encountering the above error when I use $cordovaNetwork. Any idea why? Basically I have a requirement of making ionic app run offline using sqlite…for this I need to put a check if internet is there or not
I am encountering the following error on inspection
Yes I have installed it through command line…but the Network.js and Connection.js files present in www folder give the error “Uncaught reference error: Module not defined” & “require not defined”. So I had to shun using those files
Thanks @Calendee. The link you shared works perfectly fine for Browsers. navigator.onLine detects internet availability in browser but the same is not reflected when we test in phones. In my android phone, even in airplane mode, the app says it has internet connect(with WIFI turned off). IS there an alternative for using “navigator.onLine” in angular JS?
just anobservation are you sure you have includied ng-cordova.js properly?
in index.html
<!-- ng-cordova have to be before cordova -->
<script src="lib/ng-cordova/dist/ng-cordova.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
and in you controllers module needs ngCordova:
var starterControllers = angular.module(‘starter.controllers’, [‘appServices’, ‘ngCordova’]);
i’m not sure if its to any help but the full example can be found here:
1)Approach one
myApp.controller(‘RequestAccessCtrl’, function ($scope, $rootScope, $ionicLoading, $ionicPlatform)//, $cordovaNetwork)
{
debugger
if (navigator.onLine) {
alert("Internet available ");
} else {
alert("Internet not available ");
}
});
The above code works fine in chrome but no in device. In device it always says internet available.
Approach two
myApp.controller(‘LoginCtrl’, function ($scope, $rootScope, LoginService, $ionicLoading, $ionicPopup, $ionicPlatform, $ionicModal, $cordovaNetwork)
{ //ngCordovaService)
$rootScope.User = “”;
var type = $cordovaNetwork.getNetwork();
var isOnline = $ cordovaNetwork.isOnline();
var isOffline = $ngCordovaService.isOffline();
alert(isOffline);
$ionicModal.fromTemplateUrl(‘templates/modal-Login.html’, function ($ionicModal) {
});
The above code fails when I append $cordovaNetwork. We also tried using ngCordovaService. This also fails in the browser.
http://snag.gy/j9h91.jpg is the link for screenshot when I use ngCordova. I followed the following steps. Correct me if I am wrong:
ngCordova Custom Build and clicked on ‘network information’,built it. It downloaded “ngCordova.js” [please note the filename. its not ng-cordova.js as mentioned in the docs]
I added this file in lib/ionic/js path
In my controller, I appended [ngCordova]
Now when I run my app in the browser, it shows the attached error
Thanks @Bonda I did build the app in the phone using ionic build android. With/Without ngCordova I see no output. Please see my steps in the edited question
Can you kindly post something on codepen which is working source code @pahamad?
To cross verify my issue I tried installing another plugin cordova plugin add org.apache.cordova.dialogs for dialogs. I still encounter the issue but the error now says “TypeError: Cannot read property ‘alert’ of undefined” I referred the cordovaDialog example mentioned in the docs