How do you transform a javascript function into an angular "function"?

0

I have the following code:

        angular.module("contato", []);
        angular.module("contato").controller("contatoCtrl", function($scope){            
            $scope.app = "contatos";

        });

How to use a normal javascript function in angular format? For example:

function checkConnection() {
    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]);
}

Turning or not angular, this way:

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

                $scope.app = "+contato";

                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();

            });

The error remains:

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)(anonymous function) @ angular.js:12450
    
asked by anonymous 25.04.2016 / 01:12

1 answer

3
$scope.checkConnection = function () {

...
}

In your view you can access the function, starting by clicking or clicking a button, for example:

<div init="checkConnection()"></div> //inicia ao carregar a view

<button ng-click="checkConnection()"></button> //inicia com um clique no botão
    
25.04.2016 / 01:59