简体   繁体   English

Angular JS:控制器中未定义函数错误

[英]Angular JS: Function is not defined error in controller

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我从另一个函数initialize()调用了函数currentLocation() ,我定义了此功能,但它给我一个函数未定义的错误:\\ 代码已更新,但仍然发生相同的错误有人可以告诉我这是什么吗?我的代码有问题吗?

HomeCtrl.js 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;               
            });
};

}]);

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

$scope.currentLocation definition should be inside module. $scope.currentLocation定义应位于模块内部。

Currently you are writing currentLocation outside module, hence it is not accessible. 当前,您正在模块外部编写currentLocation,因此无法访问它。

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM