2013-03-01 21 views
13

Tôi có nhiều đường dẫn URL mà tôi muốn ánh xạ tới một tài nguyên. Tuy nhiên, tôi không chắc chắn cách thay đổi URL dựa trên hàm được gọi. Ví dụ: bản đồ dest cho truy vấn sẽ là/allProducts, tuy nhiên phá hủy sẽ là một cái gì đó dọc theo dòng/xóa /: idNhiều url định tuyến cho một dịch vụ duy nhất AngularJS

service.factory('ProductsRest', ['$resource', function ($resource) { 
    return $resource('service/products/:dest', {}, { 
     query: {method: 'GET', params: {}, isArray: true }, 
     save: {method: 'POST'}, 
     show: { method: 'GET'}, 
     edit: { method: 'GET'}, 
     update: { method: 'PUT'}, 
     destroy: { method: 'DELETE' } 
    }); 
}]); 

Trả lời

24

Đối với mỗi hành động bạn có thể ghi đè đối số url. Đặc biệt cho điều này là đối số url: {...}.

Trong ví dụ của bạn:

service.factory('ProductsRest', ['$resource', function ($resource) { 
    return $resource('service/products/', {}, { 
     query: {method: 'GET', params: {}, isArray: true }, 
     save: {method: 'POST', url: 'service/products/modifyProduct'}, 
     update: { method: 'PUT', url: 'service/products/modifyProduct'} 
    }); 
}]); 
17

Tôi chỉ cần thiết để đưa các url trong khi param.

service.factory('ProductsRest', ['$resource', function ($resource) { 
    return $resource('service/products/:dest', {}, { 
     query: {method: 'GET', params: {dest:"allProducts"}, isArray: true }, 
     save: {method: 'POST', params: {dest:"modifyProduct"}}, 
     update: { method: 'POST', params: {dest:"modifyProduct"}}, 
    }); 
}]); 
+0

làm thế nào bạn gọi mỗi người trong số này? giả sử bạn chỉ muốn gọi lưu, bạn đã làm ProductsRest.save(); ? – user20358

+1

giải pháp tốt đẹp nhưng muốn giải pháp mikemueller, đối với một số trường hợp, nó rõ ràng hơn và dễ đọc hơn. –

Các vấn đề liên quan