2015-09-20 53 views
0

這是我第一個角度js應用程序。我正在嘗試發出http請求並取回響應。

app.service('info',['$http',function($http){ 

       var details=function (query) { 
          $http({ 
          method:'GET', 
          url:'http://api.openweathermap.org/data/2.5/forecast/daily', 
          params:{q:query,cnt:8} 
          }).then(function (response){ 

          console.log(response.data); 
          return response.data; 

          },function (response){ 
           console.log('s'); 
          }); 
         }; 

       return {details:details} 

      }]); 

這是我的控制器:

app.controller("search",['$scope','$routeParams','info',function($scope,$routeParams,info){ 

      if ($routeParams.param) { 
       var d=info.details($routeParams.param); 
        d.then(function (r){ 
         console.log("success"); 
        },function (e){ 
         console.log("fail"); 
        }); 
}]); 

但在使用它顯示「Cannot read property 'then' of undefined"。我已經在這裏閱讀到類似的問題的解決方案,這是在很多問題可行的解決方案,但我不`噸得到哪裏去了可怕的錯誤

也不要解釋這種說法:

return {details:details} 
+0

回報{細節:細節}:該信息服務返回一個對象常量詳細信息(姓名):細節(值).. –

+0

最後一條語句基本上使得公衆的'details'功能,這樣你就可以從調用它控制器。否則它只是您的服務功能中的一個變種,並且由於範圍限制而處於無法觸及的封閉範圍內 – charlietfl

回答

3

在你的details函數中,你需要返回$ http()。

var details=function (query) { 
     return $http(...); 
    };