0

In my HomeCtrl.js I am calling function currentLocation() from another function initialize() , I defined this fuction but it gives me an error of function is not defined :\ Code Updated But still same error occurred Can anybody tell me what is the problem in my code?

HomeCtrl.js

'use strict';
angular.module('Home').controller('HomeCtrl',['$scope','$state','MessageService', function($scope, $state, $ionicModal, MessageService) {

    (function initialize(){
      var location = $scope.currentLocation();
      $scope.mapOptions = {
        mapTypeControl: true,
        zoom: 15,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: location
      };
    })();
$scope.currentLocation = function(){

navigator.geolocation.getCurrentPosition(function(pos) {
                $scope.position = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
                return $scope.position;               
            });
};

}]);

3 Answers 3

1

Put it on top of the self-invoking function

'use strict';
angular.module('Home', []).controller('HomeCtrl', function($scope) {
    $scope.currentLocation = function() {

        navigator.geolocation.getCurrentPosition(function(pos) {
            $scope.position = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
            return $scope.position;
        });
    };

    (function initialize() {
        var location = $scope.currentLocation();
        $scope.mapOptions = {
            mapTypeControl: true,
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            center: location
        };
    })();

});

'use strict';
angular.module('Home', []).controller('HomeCtrl', function($scope) {
    $scope.currentLocation = function() {
    console.log("working")

        navigator.geolocation.getCurrentPosition(function(pos) {
            $scope.position = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
            return $scope.position;
        });
    };

    (function initialize() {
        var location = $scope.currentLocation();
        $scope.mapOptions = {
            mapTypeControl: true,
            zoom: 15, 
            center: location
        };
    })();

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="Home" ng-controller="HomeCtrl">
 
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

$scope.currentLocation definition should be inside module.

Currently you are writing currentLocation outside module, hence it is not accessible.

Comments

1

You should have added empty dependency to your module

Change

From

 angular.module('Home').controller('HomeCtrl',

To

 angular.module('Home',[]).controller('HomeCtrl',

Also the order of parameters is wrong, change it as

'use strict';
angular.module('Home').controller('HomeCtrl', ['$scope', '$state','$ionicModal', 'MessageService', function ($scope, $state, $ionicModal, MessageService) {
   $scope.currentLocation = function() {
console.log("working")

    navigator.geolocation.getCurrentPosition(function(pos) {
        $scope.position = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
        return $scope.position;
    });
};

(function initialize() {
    var location = $scope.currentLocation();
    $scope.mapOptions = {
        mapTypeControl: true,
        zoom: 15, 
        center: location
    };
})();
}]);

2 Comments

I have update my code Please check ... still same error ocurred
@mariasalahuddin check with the updated controller in the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.