在我HomeCtrl.js
我从另一个函数currentLocation()
调用函数initialize()
时,我定义了这个函数,但它给了我一个函数错误没有定义:\
代码已更新但仍发生相同的错误
谁能告诉我代码中的问题是什么?
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;
});
};
}]);
答案 0 :(得分:1)
您应该为模块添加空依赖
更改
这
angular.module('Home').controller('HomeCtrl',
要
angular.module('Home',[]).controller('HomeCtrl',
参数的顺序也是错误的,将其更改为
'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
};
})();
}]);
答案 1 :(得分:1)
$scope.currentLocation
定义应该在模块内部。
目前您正在编写模块外的currentLocation,因此无法访问它。
答案 2 :(得分:1)
将它置于自我调用函数
之上'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>