gpt4 book ai didi

javascript - 加载 View 时,angularjs GET url

转载 作者:行者123 更新时间:2023-11-28 15:03:39 25 4
gpt4 key购买 nike

在加载我的 View 之一时尝试进行 API 调用。

controllers.controller("detailsCtrl", ["$scope", "$routeParams", "$filter", "$http", function($scope, $routeParams, $filter, $http) {

$scope.getCurrent = function(url, id, callBack, apiCall, promise) {
var url = "api.openweathermap.org/data/2.5/weather?id=2172797";
var id = "&appid=d436c04d23a5a44329eb8255190a84be";
var callBack = "&callback=JSON_CALLBACK";
var apiCall = url += id += callBack;
var promise = $http.jsonp(apiCall);
promise.success(function(response) {
$scope.current = response.current;
console.log($scope.current);
});
};

$scope.cityName = $filter("filter")($scope.data.list, {
id: $routeParams.cityId
})[0];

}]);

在 html 中使用 ng-init

      <div ng-controller="detailsCtrl" ng-init="getCurrent()">
<h1>{{ cityName.name }}</h1>
<tr>
<td>lat: {{cityName.coord.lat}}, lon: {{cityName.coord.lon}}</td>
<td>{{}}</td>
<td>{{}}</td>
<td>{{}}</td>
<td>{{}}</td>
<td>Clouds: {{cityName.clouds.all}}</td>
<td>{{}}</td>
<td>{{}}</td>
</tr>

</div>

继续获取:http://localhost:8080/api.openweathermap.org/data/2.5/weather?id=2172797&appid=d436c04d23a5a44329eb8255190a84be&callback=angular.callbacks._1 ,有什么想法吗?

注意 在用户导航到详细 View 之前,还在我的主视图中进行另一个 API 调用。当用户切换 View 时尝试 ng-click,但它根本没有进行调用。

最佳答案

我使用的是 Angular2 而不是 AngularJS,但相信你的问题与不正确的 http 请求更相关。我刚刚在 plunkr 中计算出你的样本:http://plnkr.co/edit/rOZOyAcAuVcTr3njqKfH?p=preview

getData: function () {
var obj = {};
var jsonData = JSON.stringify({ obj: obj }); //For POST
var req = {
method: 'POST',
url: 'http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=d436c04d23a5a44329eb8255190a84be',
};
return this.http.post(req.url, req.data, req)
.toPromise()
.then(function (response) {
return response;

});
}

您的网址与您的解决方案相关可能有一个明显的原因。

希望这会有所帮助。

谢谢

关于javascript - 加载 View 时,angularjs GET url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40110726/

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