2014-09-23 56 views
0

我是AngularJs的新手。這是我的代碼。在這我想通過獲取系統日期來建立網址。但這些值在jsonp請求時不會被替換。我不知道爲什麼。變量值在運行時不會被替換爲角js

在這裏,當我嘗試手動添加參數。我正在獲取價值。但是,當我嘗試使用變量,我沒有得到輸出。

$scope.getCurrentMonthData = function(){ 

     $scope.date = new Date(); 
     $scope.startday = 1; 
     $scope.currentday = $scope.date.getDate()+1; 
     $scope.month = $scope.date.getMonth()+1; 
     $scope.year = $scope.date.getFullYear(); 
     $scope.start= $scope.year+'/'+$scope.month+'/'+$scope.startday; 

     $scope.end = $scope.year+'/'+$scope.month+'/'+$scope.currentday; 

     $scope.url1 = $rootScope.ApiWeatherHost+'/climo/v1/actuals/'+$scope.location_key+'.json?start='+$scope.start+'&end='+$scope.end+'&apikey='+$rootScope.ApiKey; 

     $scope.url2 = $rootScope.ApiWeatherHost+'/climo/v1/records/'+$scope.location_key+'.json?start='+$scope.start+'&end='+$scope.end+'&apikey='+$rootScope.ApiKey; 

     $scope.url3 = $rootScope.ApiWeatherHost+'/climo/v1/normals/'+$scope.location_key+'.json?start='+$scope.start+'&end='+$scope.end+'&apikey='+$rootScope.ApiKey; 

     $http.jsonp($scope.url1).then(function(msg){ 
      console.log(msg.data); 
     }); 
     $http.jsonp($scope.url2).then(function(msg){ 
      console.log(msg.data); 
     }); 
     $http.jsonp($scope.url3).then(function(msg){ 
      console.log(msg.data); 
     }); 

    }; 

回答

0

我不知道你在哪裏定義location_key,所以我不知道某些如果這是你的問題,但是當我運行下面的代碼,我看到創建三個URL的。

您是否檢查過試圖發出這些請求時是否有錯誤?

(function($scope, $rootScope){ 
 

 
     $scope.date = new Date(); 
 
     $scope.startday = 1; 
 
     $scope.currentday = $scope.date.getDate()+1; 
 
     $scope.month = $scope.date.getMonth()+1; 
 
     $scope.year = $scope.date.getFullYear(); 
 
     $scope.start= $scope.year+'/'+$scope.month+'/'+$scope.startday; 
 

 
     $scope.end = $scope.year+'/'+$scope.month+'/'+$scope.currentday; 
 

 
     $scope.url1 = $rootScope.ApiWeatherHost+'/climo/v1/actuals/'+$scope.location_key+'.json?start='+$scope.start+'&end='+$scope.end+'&apikey='+$rootScope.ApiKey; 
 

 
     $scope.url2 = $rootScope.ApiWeatherHost+'/climo/v1/records/'+$scope.location_key+'.json?start='+$scope.start+'&end='+$scope.end+'&apikey='+$rootScope.ApiKey; 
 

 
     $scope.url3 = $rootScope.ApiWeatherHost+'/climo/v1/normals/'+$scope.location_key+'.json?start='+$scope.start+'&end='+$scope.end+'&apikey='+$rootScope.ApiKey; 
 

 
     var output = $('#output'); 
 
     $('<div>').text($scope.url1).appendTo(output); 
 
     $('<div>').text($scope.url2).appendTo(output); 
 
     $('<div>').text($scope.url3).appendTo(output); 
 
     
 

 
}({}, {ApiWeatherHost:'www.fake-api.com', ApiKey:'12345'}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id='output'></div>

+0

雅我也得到這個。但是當我嘗試發送該網址到JSNOP .. 它不工作..我不知道爲什麼。? – mani 2014-09-23 16:30:00

+0

@mani - 它在開發工具控制檯中說了什麼?如果您檢查返回的HTTP響應是否包含錯誤? – Josh 2014-09-23 17:05:20

+0

輸出即將到來。但它不是JSONP格式。它只給出JSON格式。有時候它也會將JSONP格式作爲輸出。我不知道這件事。我也檢查服務器端的人。從他們的最終沒有問題。 – mani 2014-09-23 17:37:45