Znowman Znowman - 1 year ago 73
AngularJS Question

angularjs GET url when loading view

Trying to make an API call when loading one of my views.

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;

$scope.cityName = $filter("filter")($scope.data.list, {
id: $routeParams.cityId


using ng-init in the html

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


Keep getting: http://localhost:8080/api.openweathermap.org/data/2.5/weather?id=2172797&appid=d436c04d23a5a44329eb8255190a84be&callback=angular.callbacks._1 , any ideas why?

note Also making another API call in my mainview before the user navigates to the detail view. Tried ng-click when the user switches view, but it didnt make the call at all.

Answer Source

Please change the url as below and

var url = "http://api.openweathermap.org/data/2.5/weather?id=2172797";

as your not able to find the url. In your call it will make the url to get appended from the current URL.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download