gpt4 book ai didi

javascript - Angular JS $http 请求?

转载 作者:行者123 更新时间:2023-11-28 00:31:01 25 4
gpt4 key购买 nike

我正在尝试使用 Angular js 发出 $http 请求,以从谷歌地图获取 json 对象。

$http.get('http://maps.googleapis.com/maps/api/geocode/json?address=' + data[ 'street' ] + ',' + data[ 'city' ] + ',Deutschland' + '&sensor=true').success(function(mapData) {
angular.extend($scope, mapData);
});

我读到我需要先“注入(inject)”$http,但我就是无法理解它是如何工作的?我尝试做这样的事情:

angular.module('wmw', [])
.run(['$scope', '$http', function($scope, $http){
function getTargetCords(data, $scope, $http) {
var city = data[ 'city' ];
return $http.get('http://maps.googleapis.com/maps/api/geocode/json?address=' + data[ 'street' ] + ',' + data[ 'city' ] + ',Deutschland' + '&sensor=true').success(function(mapData) {
angular.extend($scope, mapData);
});
}
}]);

但在这种情况下,当我尝试在此之外使用它时,它会说“getTargetCords 未定义”。我尝试了多种不同的解决方案,但无法弄清楚如何让它发挥作用。

编辑:我认为我需要这个函数的原因可能很令人困惑,所以这是我的其他代码:

var onSuccess = function(position) {
currentLat = position.coords.latitude ;
currentLng = position.coords.longitude;
var thecoords = [];
$('#filter-list').empty();
for(i = 0; i<locations.length;i++){
thecoords = getTargetCords(locations[i]);
var distance = calculateDistance(currentLat, currentLng, thecoords[0], thecoords[1]);
addItemToList(locations[i], distance);
}
};

// onError Callback receives a PositionError object
function onError(error) {
alert('Aktueller Standort konnte nicht berechnet werden');
}

navigator.geolocation.getCurrentPosition(onSuccess, onError);

我们有不同的地点,我需要每个地点的距离。

请注意:由于我没有完全完成 Angular 部分,“thecoords[0]”、“thecoods[1]”现在显然是错误的值

最佳答案

更新2

http://pastebin.com/WUuYFAnX

当我们希望从旧应用程序中调用 AngularJS 代码时,请将 AngularJS 代码视为存在于旧应用程序中 protected 容器内的小型应用程序;您不能直接调用它,但您可以进行远程调用。

您可以使用 Controller 所在的 HTML 元素的 ID。 (不推荐)但您可以执行以下操作:

HTML 更改,我们需要 ng-controllerng-app

<html ng-app="wmw">
<body ng-controller="MainCtrl" id="MainCtrlId">
...
</body>
</html>

Angular 代码和原生 JS

var app = angular.module('wmw', []);

app.controller('MainCtrl', function ($scope, $http) {

$scope.getTargetCords = function (data) {

$http.get(data).success(function (response) {
var responseData = JSON.parse(response);
console.log(responseData);
});
};
});



function getCords() {
var city = activeDest['city'];
var destUrl = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' + activeDest['street'] + ',' + activeDest['city'] + ',Deutschland' + '&sensor=true';

var MyAngularScope = angular.element($("#MainCtrlId")).scope();
//Now call your angular method.
MyAngularScope.getTargetCords(destUrl);
}

有关此技术的更多详细信息,请参阅此 JSFiddle Example .

强烈建议您重做应用程序以使其在 Angular 生态系统中工作,而不是执行上述操作。

这是一个简单的 Angular 设置,将 Angular Controller 分离到它们的 JS 文件中,并将它们导入到您的 HTML 中,就像使用任何其他 JS 文件。

var app = angular.module('wmw', []);
app.controller('YourCtrlName', function ($scope, $http) {

//Inside your controller you can define your "scope functions".
$scope.getTargetCords= function(){

$http.get('urlToGet').success(function(response) {
var responseData = JSON.parse(response);
console.log(responseData);
});

};
});

现在在你的 HTML 中你可以有这样的东西:

<div ng-app="wmw">
<div ng-controller="YourCtrlName">
<button ng-click="getTargetCords()">GET DATA</button>
</div>
</div>

您收到:

getTargetCords is not defined

因为您尝试从 Angular 应用程序外部访问 Angular Controller 方法。

关于javascript - Angular JS $http 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28990588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com