2016-08-10 6 views
2

を取得し、私の角度サービスです:

angular 
    .module('myApp') 
    .factory('partyService', function($http) { 
     this.fetch = function() { 
      return $http.get('/api/parties') 
         .then(function(response) { 
          return response.data; 
         }); 
     } 
    }); 

私は、このサービスがデータを返している(または空の配列が//わからない)と思います

そして、なぜ私はこのエラーをgeetingています:

Error: [$injector:undef] Provider 'partyService' must return a value from $get factory method.

更新:

私は工場の代わりにサービスを使用してもエラーは発生しません。なぜそうなのか???

答えて

3

エラーはそれ自体について語ります。あなたはあなたの工場で何かを返す必要があります。

var factory = { 
    fetch: fetch 
}; 

return factory; 

function fetch() { 
    return.. 
} 

次に、あなたのcontrollerに:非常にうまく機能

partyService.fetch()... 
+1

うん、。ありがとうございました。私の工場が何も返さなかったことは決して知らなかった。私は工場内に2つのリターン・ステートメントがあると思っていたので、データを戻しています。 – Vishal

0
angular 
.module('myApp') 
.factory('partyService', function($http) { 

    function fetch = function() { 
     return $http.get('/api/parties') 
        .then(function(response) { 
         return response.data; 
        }); 
    } 

    function anotherFetch = function() { 
     return $http.get('/api/parties') 
       .then(function(response) { 
        return response.data; 
       }); 
    } 

    return { 
    fetch, 
    anotherFetch, 
    } 


}); 
関連する問題