2

我有一个为我提供员工和部门的 RESTful API。在我的 AngularJS 应用程序中,我需要知道员工和员工所属的部门。我为此定义了两个工厂服务:

angular.module('myApp.services', [])
    .factory('Employee', function ($resource) {
        return $resource ('/api/employees/:id', { id: '@id' });
    })
    .factory('Department', function ($resource) {
        return $resource ('/api/departments/:id', { id: '@id' });
    })
;

在控制器中,我调用:

$scope.employee = Employee.get({id: 'me'});

接下来,我想调用:

$scope.departments = [];
angular.forEach($scope.employee.departmentIds, function (departmentId) {
    $scope.departments.push(Department.get({ id: departmentId }));
});

但是$scope.employee是一个promise对象,所以$scope.employee.departmentIds只有在第一个请求完成时才会知道。

我可以这样解决它:

Employee.get({id: 'me'}).$promise.then(function (employee) {
    $scope.employee = employee;
    $scope.departments = [];
    angular.forEach(employee.departmentIds, function (departmentId) {
        $scope.departments.push(Department.get({ id: departmentId }));
    });
});

问题是我在多个控制器中需要此信息,并且不想在每个控制器中重复此代码。所以我的问题是:有没有办法将这两种资源结合起来作为一种服务,既提供员工数据又提供该员工所属部门的数据?

4

2 回答 2

2

你可以这样做:

angular.module('myApp.services', [])
    .factory('SimpleEmployee', function ($resource) {
        return $resource ('/api/employees/:id', { id: '@id' });
    })
    .factory('Department', function ($resource) {
        return $resource ('/api/departments/:id', { id: '@id' });
    })
    .factory('Employee', ['SimpleEmployee', 'Department', function(SimpleEmployee, Department) {
        return {
            get: function(params) {
                var data = SimpleEmployee.get({id: params.id}, function (employee) {
                    var obj = {
                        employee: employee,
                        departments: []
                    };                        
                    angular.forEach(employee.departmentIds, function (departmentId) {
                        obj.departments.push(Department.get({ id: departmentId }));
                    });
                    return obj;
                });
                return data;
            }
        }
    }]);

而且您不需要更改控制器中的任何内容。

于 2013-11-29T16:33:25.637 回答
1

此答案基于@Beterraba 的答案(但该答案中的代码示例不正确):

服务:

angular.module('myApp.services', [])
    .factory('SimpleEmployee', function ($resource) {
        return $resource ('/api/employees/:id', { id: '@id' });
    })
    .factory('Department', function ($resource) {
        return $resource ('/api/departments/:id', { id: '@id' });
    })
    .factory('Employee', ['SimpleEmployee', 'Department', function(SimpleEmployee, Department) {
        return {
            get: function(params) {
                return SimpleEmployee.get({id: params.id}).$promise.then(function (employee) {
                    employee.departments = [];
                    angular.forEach(employee.departmentIds, function (departmentId) {
                        employee.departments.push(Department.get({ id: departmentId }));
                    });
                    delete employee.departmentIds;
                    return employee;
                });
            }
        }
    }]);

控制器:

Employee.get({ id: 'me' }).then(function (employee) {
    $scope.employee = employee;
});

从 AngularJS 1.2 RC3 开始,您需要解开控制器上的所有承诺对象。我在这里找到了这些信息。

于 2013-12-05T11:15:50.167 回答