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

Asked

Viewed 622 times

0

I have the following Angular 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 turning into angular, thus:

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
  • I did not understand your question well... But Angularjs is JS, IE, there is nothing wrong with its function. If I understood the point you want to reach, that would be to pass the values to the angular, you can do at the end. For example: $scope.states = states; and all property of var states will be passed to Scope.

  • Just complementing Celsom, as he said, Angularjs is Javascript, no more. The fact that you are using the Angular framework does not mean that all its functions, for example, will be in $Scope. It’s common to see a lot of people putting everything that is a function in $Scope because they think it’s a "default" that should be followed by the framework.

1 answer

3

$scope.checkConnection = function () {

...
}

in your Voce view you can access the function by starting by pressing 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.