Javascript angularjs在加载视图时获取url
加载我的一个视图时尝试进行API调用Javascript angularjs在加载视图时获取url,javascript,angularjs,Javascript,Angularjs,加载我的一个视图时尝试进行API调用 controllers.controller("detailsCtrl", ["$scope", "$routeParams", "$filter", "$http", function($scope, $routeParams, $filter, $http) { $scope.getCurrent = function(url, id, callBack, apiCall, promise) { var url = "api.openwea
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>
{{cityName.name}
lat:{{cityName.coord.lat},lon:{{cityName.coord.lon}
{{}}
{{}}
{{}}
{{}}
云:{{cityName.Clouds.all}
{{}}
{{}}
不断得到:,有什么想法吗?为什么
请注意,在用户导航到详细视图之前,在my mainview中进行另一个API调用。当用户切换视图时尝试单击,但根本没有呼叫。请更改url,如下所示
var url = "http://api.openweathermap.org/data/2.5/weather?id=2172797";
因为您无法找到url。
在您的呼叫中,它将使url从当前url追加。请更改url,如下所示
var url = "http://api.openweathermap.org/data/2.5/weather?id=2172797";
因为您无法找到url。
在您的调用中,它将使url从当前url追加。我使用的是angular2而不是angularjs,但我相信您的问题更多地与不正确的http请求有关。我刚刚在plunkr中计算出了您的样本: 可能有一个明显的原因是您的url相对于您的解决方案 希望这会有所帮助
谢谢我使用的是angular2而不是angularjs,但我相信您的问题更多地与不正确的http请求有关。我刚刚在plunkr中计算出了您的样本: 可能有一个明显的原因是您的url相对于您的解决方案 希望这会有所帮助
谢谢您是否尝试过在路由上的resolve参数处进行调用?检查请求是否激发到正确的url?在浏览器中测试url,并正常工作。编辑:api支持jsonp吗?您可以添加html代码吗。我无法理解您到底想做什么?您是否尝试过在路由上的resolve参数处进行调用?检查请求是否激发到正确的url?在浏览器中测试url,并正常工作。编辑:api支持jsonp吗?您可以添加html代码吗。我不能完全理解你想要做什么?好的,放弃http,现在我在控制台中没有定义,只有第一个API调用记录在控制台中。是的,标记了它,参数中的http://和(响应)应该设置为当前。这不是你的问题!好的,放弃http,现在我在控制台中没有定义,只有第一个API调用记录在控制台中。是的,标记了它,参数中的http://和(响应)应该设置为当前。这不是你的问题!