0

我正在尝试为 $resource 定义一个自定义 getUsers 操作,但我收到一个错误,即该对象没有 getUsers 方法。

angular.module('myApp')
    .factory('Customer', ['$resource', 'apiUrl', function ($resource, apiUrl) {
        return $resource(apiUrl + '/customers/:id', {
            query: {method: 'GET', isArray: true},
            get: {method: 'GET'},
            getUsers: {method: 'GET', url: apiUrl + '/customers/:id/users', isArray: true}
        });
    }]);

与它一起使用Customer.getUsers({id: 'id'})

我正在使用 1.2.0-rc2。我在这里想念什么?

4

1 回答 1

1
 angular.module('myApp')
  .factory('Customer', ['$resource', 'apiUrl', function ($resource, apiUrl) {
    return $resource(apiUrl + '/customers/:id', {
        query: {method: 'GET', isArray: true},
        get: {method: 'GET'},
        getUsers: {method: 'GET', url: apiUrl + '/customers/:id/users', isArray: true}
    });
}]);

替换为:

angular.module('myApp')
  .factory('Customer', ['$resource', 'apiUrl', function ($resource, apiUrl) {
     return $resource(apiUrl + '/customers/:id', { id: 'id' }, { //here add { }
        query: {method: 'GET', isArray: true},
        get: {method: 'GET'},
        getUsers: {method: 'GET', url: apiUrl + '/customers/:id/users', isArray: true}
    });
}]);
于 2013-09-24T15:56:46.077 回答