我试图通过ajax调用获取angularjs中的用户位置,但我收到语法错误
app.controller('locationController', function($scope, $window) {
$window.navigator.geolocation.getCurrentPosition(function(position) {
console.log(position);
var lat = position.coords.latitude;
var long = position.coords.longitude;
$scope.$apply(function() {
$scope.lat = lat;
$scope.long = long;
});
});
var city = function() {
http.get("http://maps.googleapis.com/maps/api/geocode/json?latlng="
lat ","
long "&sensor=true").success(function(response) {
console.log(response);
});
}
});

我得到的错误是令牌上的语法错误,删除此令牌
答案 0 :(得分:0)
试试这个,需要添加$ http .. angularjs支持ajax的2个方法。一个是严格的函数调用(4个动作类型,get,post,update,delete)或添加一个配置对象,如$ .ajax。
app.controller('locationController', function($scope, $window, $http) { // updated
var city = function() {
$http.get("http://maps.googleapis.com/maps/api/geocode/json?latlng=" // updated
lat ","
long "&sensor=true").success(function(response) {
console.log(response);
});
}
}
答案 1 :(得分:0)
我发现至少有三个问题:
http.get
电话中似乎没有正确连接字符串。 lat
和long
超出了city
功能的范围。您可能打算使用$scope.lat
和$scope.long
? http
是一项服务而您没有注入它。
app.controller('locationController', function($scope, $window, $http) {
$window.navigator.geolocation.getCurrentPosition(function(position) {
console.log(position);
var lat = position.coords.latitude;
var long = position.coords.longitude;
$scope.$apply(function() {
$scope.lat = lat;
$scope.long = long;
});
});
var city = function() {
$http.get("http://maps.googleapis.com/maps/api/geocode/json?latlng="
+ $scope.lat + ","
+ $scope.long + "&sensor=true").success(function(response) {
console.log(response);
});
}
});