Error in function cordova and AngularJS

0

Below is the code I'm using. It is the standard example of the Cordova connection checking function.

    <script>
        angular.module("contato", []);
        angular.module("contato").controller("contatoCtrl", function($scope){

            $scope.app = "app";

            var checkConnection = function () {
                var networkState = navigator.connection.type;
                var states = {};
                states[Connection.UNKNOWN]  = 'Unknown connection';
                states[Connection.ETHERNET] = 'Ethernet connection';
                states[Connection.WIFI]     = 'WiFi connection';
                states[Connection.CELL_2G]  = 'Cell 2G connection';
                states[Connection.CELL_3G]  = 'Cell 3G connection';
                states[Connection.CELL_4G]  = 'Cell 4G connection';
                states[Connection.CELL]     = 'Cell generic connection';
                states[Connection.NONE]     = 'No network connection';

                console.log('Connection type: ' + states[networkState]);
                alert('Connection type: ' + states[networkState]);


            };

            checkConnection();

        });

    </script>

However, I get the following error:

angular.js:12450 TypeError: Cannot read property 'type' of undefined
    at checkConnection (index.html:50)
    at new <anonymous> (index.html:67)
    at Object.invoke (angular.js:4476)
    at extend.instance (angular.js:9127)
    at nodeLinkFn (angular.js:8239)
    at compositeLinkFn (angular.js:7671)
    at publicLinkFn (angular.js:7546)
    at angular.js:1662
    at Scope.$eval (angular.js:15922)
    at Scope.$apply (angular.js:16022)

Note: If the solution is to remove the angular function and perform as a normal javascript function, you will also be welcome

    
asked by anonymous 07.05.2016 / 14:56

1 answer

0

Hello,

If navigator.connection is undefined you probably forgot to add the connection plugin in the cordova:

link

    
09.06.2016 / 16:51