工厂中工厂的角度依赖注入返回空对象

时间:2014-11-26 23:37:41

标签: angularjs dependency-injection

所以我有这个非常奇怪的问题,我不能将一个工厂注入另一个工厂,而只能在一个特定工厂注入。

所以我有以下代码..

控制器:

angular.module('cheetah').controller("NewSprintController", ["$rootScope", "$scope", "$state", "EventService", "$modalInstance", "$timeout", "ErrorService", "NewSprintLogicService", "EventIdentifierService", function NewSprintController($rootScope, $scope, $state, evs, $modalInstance, $timeout, err, nsl, eis) {
    $scope.Sprint = nsl.CreateSprint();
    ...

我注入NewSprintLogicService的地方,我将其解析为nsl。这很好用,如果我们再查看NewSprintLogicService,它看起来像这样:

angular.module("cheetah").factory("NewSprintLogicService", ["ApiService", function NewSprintLogicService(api) {
return {
    CreateSprint: function () { return api.Sprint(); },
    GetAll: function() {
    ...

现在,我在这里注入我的ApiService,这就是它变得棘手的地方。我正在注入一个空物体。这种模式适用于不同的控制器和逻辑服务。 Sprint属性实际上是在ApiService

中定义的

然后澄清一下,这是我的ApiService:

angular.module("cheetah").factory("ApiService", ["$resource", function ApiService($resource) {
return {
    UserStory: $resource("/api/UserStory/:userStoryId", { userStoryId: "@Id" }),
    ...

在尝试在LogicService中使用ApiService时,除了“无法读取未定义属性”和“未定义不是函数”之外,我没有收到任何错误。

1 个答案:

答案 0 :(得分:0)

确实,尝试将问题表达为Stackoverflow问题会有所帮助。在这种情况下,我突然意识到我错过了一个新的"在我的LogicService中,所以它应该是这样的:

angular.module("cheetah").factory("NewSprintLogicService", ["ApiService", function NewSprintLogicService(api) {
return {
    CreateSprint: function () { return new api.Sprint(); },
    ...

简单的错误。