单个服务AngularJS的多个路由URL

时间:2013-03-01 15:35:26

标签: rest angularjs

我想要将多个网址路径映射到单个资源。但是我不确定如何根据调用的函数更改URL。例如:查询的dest映射将是/ allProducts,但是destroy将是/ delete /:id

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

2 个答案:

答案 0 :(得分:25)

对于每个操作,您可以覆盖url参数。 特别是url: {...}参数。

在您的示例中:

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

答案 1 :(得分:18)

我只需将url作为参数。

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