Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用参数在angularjs中进行ajax调用_Javascript_Ajax_Angularjs - Fatal编程技术网

Javascript 如何使用参数在angularjs中进行ajax调用

Javascript 如何使用参数在angularjs中进行ajax调用,javascript,ajax,angularjs,Javascript,Ajax,Angularjs,我试图通过ajax调用获取angularjs中的用户位置,但我发现语法错误 应用程序控制器'locationController',函数$scope,$window{ $window.navigator.geolocation.getCurrentPositionfunctionposition{ 控制台。日志位置; var lat=位置坐标纬度; var long=位置坐标经度; $scope.$applyfunction{ $scope.lat=lat; $scope.long=long;

我试图通过ajax调用获取angularjs中的用户位置,但我发现语法错误

应用程序控制器'locationController',函数$scope,$window{ $window.navigator.geolocation.getCurrentPositionfunctionposition{ 控制台。日志位置; var lat=位置坐标纬度; var long=位置坐标经度; $scope.$applyfunction{ $scope.lat=lat; $scope.long=long; }; }; var城市=功能{ http。gethttp://maps.googleapis.com/maps/api/geocode/json?latlng= 拉特, long&sensor=true.successfunctionresponse{ console.logresponse; }; }
}; 尝试此操作,需要添加$http。。angularjs支持两种ajax方法。一种是严格的函数调用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);
    });
  }
}

我至少看到三个问题:

在http.get调用中,似乎没有正确连接字符串。 据我所知,lat和long超出了城市功能的范围。您可能打算使用$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);
    });
  }
});

首先,您必须从正确的字符串连接开始。foo somevar foobar不是javascript中串接字符串的方式。这当然是问题之一,但代码的错误远不止这些。我没有彻底阅读他的代码。。但底线是,$http是必需的!谢谢你的快速回复