【问题标题】:Angular.js HTTP Concatenation - Changing URL - Breaks AngularAngular.js HTTP 连接 - 更改 URL - 破坏 Angular
【发布时间】:2026-01-09 05:50:02
【问题描述】:

更新。据我了解,在这种情况下,连接并不是真正的重复。

我已经尝试了下面 Baszz 提供的以下内容,但它似乎打破了角度。当我使用这个时,我在视图中显示纬度、经度和邮政编码的测试表达式会显示表达式代码:

$http.get('//api.postcodes.io/postcodes?limit=1&lon=' + 
              $geolocation.position.coords.longitude + 
              '&lat=' +  
              $geolocation.position.coords.latitude);

我正在使用 Angular 的 ngGeolocation 模块来查找用户的纬度和经度,以便使用 postcodes.io 计算出邮政编码。

纬度在:$geolocation.position.coords.latitude 经度在:$geolocation.position.coords.longitude

以下示例给出了邮政编码结果:

$http.get('//api.postcodes.io/postcodes?limit=1&lon=-0.1276250&lat=51.5033630')

我想在上面的代码行中插入经度和纬度。 如何做到这一点?

完整代码:

var myApp = angular.module('myApp', ['ngGeolocation']);

myApp.controller('geolocCtrl', ['$scope', '$geolocation', '$http',
    function($scope, $geolocation, $http) {
      $scope.$geolocation = $geolocation


          // basic usage
          $geolocation.getCurrentPosition().then(function(location) {
            $scope.location = location
          });

          // regular updates
          $geolocation.watchPosition({
            timeout: 60000,
            maximumAge: 2,
            enableHighAccuracy: true
          });


       $scope.testing = "300";

          $scope.coords = $geolocation.position.coords; // this is regularly updated
          $scope.error = $geolocation.position.error; // this becomes truthy, and has 'code' and 'message' if an error occurs

          $http.get('//api.postcodes.io/postcodes?limit=1&lon=-0.1276250&lat=51.5033630')

.success(function (data) {

           $scope.postcode = data.result[0].postcode;
           var postcode2 = $scope.postcode


        })
        .error(function (data, status) {

            console.log(data);

        });


        }]);

【问题讨论】:

  • 当 Angular “中断”并在 HTML 中显示未评估的 Angular 表达式时,通常意味着浏览器控制台上有一条错误消息,告诉您代码有什么问题:) 如果您仍然需要帮助,您应该编辑此问题并添加该错误消息。
  • ngGeolocation 模块有问题。感谢您的帮助。

标签: javascript angularjs geolocation


【解决方案1】:

根据您提供的信息,我会说:

        $http.get('//api.postcodes.io/postcodes?limit=1&lon=' + 
              $geolocation.position.coords.longitude + 
              '&lat=' +  
              $geolocation.position.coords.latitude);

【讨论】:

  • 这似乎对我不起作用 - 破坏角度(在视图中显示表达式代码。)
  • 你先给我们看一些代码或者学习更多关于AngularJS的知识怎么样?
  • 我试图保持它的相关性,但可以肯定。我已经添加了完整的代码。
  • 感谢您的帮助,这让我对这个问题有了更深入的了解。
【解决方案2】:

这是模块的问题。

【讨论】: