提供者'xx'必须从$ get factory方法返回一个值

时间:2016-08-10 22:15:17

标签: javascript angularjs

这是我的角色服务:

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

我认为这个服务正在返回数据(或者是一个空数组//不确定)

那我为什么要纠结这个错误:

  

错误:[$ injector:undef]提供者'partyService'必须从$ get factory方法返回一个值。

更新

如果我使用服务而不是工厂,那么我没有任何错误。为什么呢???

2 个答案:

答案 0 :(得分:5)

错误不言而喻。您必须返回工厂中的内容:

var factory = {
  fetch: fetch
};

return factory;

function fetch() {
  return..
}

然后,在controller

partyService.fetch()...

答案 1 :(得分:5)

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,
  }


});