如何使服务依赖于其他服务值

时间:2013-12-20 13:31:02

标签: angularjs

好的,所以我的服务依赖于用户可以在应用界面中更改的其他服务值。像这样:

app.service('Applications', ['$resource', 'URL',
    function ($resource, URL) {
        var applicationsResource = $resource(URL + '/applications/:id', { id: '@id' }, {
            query: {
                method: 'GET',
                isArray: true,
                transformResponse: function(body, header) {
                    var response = angular.fromJson(body);
                    return response.data.applications;
                }
            }
        });

        var applications = applicationsResource.query(function() {
            applications.current = applications[0];
        });

        return applications;
    }
]);

app.service('Users', ['$resource', 'URL', 'Applications',
    function ($resource, URL, Applications) {
        return $resource(URL + '/users/:id', { id: '@id' }, {
            query: {
                method: 'GET',
                isArray: true,
                headers: {
                    'User': Applications.current.username,
                    'Pass': Applications.current.password
                },
                transformResponse: function(body, header) {
                    var response = angular.fromJson(body);
                    return response.data.users;
                }
            }
        });
    }
]);

工作控制器代码示例:

app.controller('usersController', ['$scope', '$resource', 'URL', 'Applications',
    function ($scope, $resource, URL, Applications) {
        $scope.users = [];

        $scope.reload = function() {
            $scope.loading = true;
            var usersResource = $resource(URL + '/users/:id', { id: '@id' }, {
                query: {
                    method: 'GET',
                    isArray: true,
                    headers: {
                        'User': Applications.current.username,
                        'Pass': Applications.current.password
                    },
                    transformResponse: function(body, header) {
                        var response = angular.fromJson(body);
                        return response.data.users;
                    }
                }
            });
            $scope.users = usersResource.query(function() {
                $scope.loading = false;
            });
            /*
            // after injecting Users, this is what I want to do, instead of what's above
            $scope.users = Users.query(function() {
                $scope.userTable.reload();
                $scope.loading = false;
            });
            */
        };

        $scope.$watch('Applications.current', function (newApplication, oldApplication, scope) {
            if (newApplication && newApplication !== oldApplication) {
                scope.reload();
            }
        });
    }
]);

我想用我的用户服务替换那个usersResource,但这就是我现在被困住的地方。

问题在于,无论我做什么,Users服务上的Applications.current始终为null。 (我确保在控制器上确保Applications.current不为空后才使用此服务)

如果我将资源直接移动到控制器,它可以工作,但我想将它们从控制器上移开。

有关如何解决或改善此问题的任何提示?

1 个答案:

答案 0 :(得分:0)

您应该知道$resource是异步的,并且在实际收到服务器的响应并填充Users之前,您需要调用applications.current服务。这就是为Applications.current null进入Users服务的原因。

在您的情况下,我会将Uses服务用于Applications

app.service('Applications', ['$resource', 'URL', 'Users',
function ($resource, URL, Users) {
        var applicationsResource = $resource(URL + '/applications/:id', { id: '@id' }, {
            query: {
                method: 'GET',
                isArray: true,
                transformResponse: function(body, header) {
                    var response = angular.fromJson(body);
                    return response.data.applications;
                }
            }
        });

        var applications = applicationsResource.query(function() {
            applications.current = applications[0];

                     // call the Users
                     Users.query(applications.current) /**/

                    return /* ... */; 

        });

        return applications;
    }
]);