繁体   English   中英

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

[英]Provider 'xx' must return a value from $get factory method

这是我的角度服务:

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方法返回一个值。

更新:

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

这个错误不言而喻。 必须在工厂归还一些东西:

var factory = {
  fetch: fetch
};

return factory;

function fetch() {
  return..
}

然后,在你的controller

partyService.fetch()...
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,
  }


});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM