2013-09-24 91 views
0

我想定義一個自定義的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。我在這裏錯過了什麼?

回答

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} 
    }); 
}]);