且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

$resource url 中的查询字符串

更新时间:2023-02-26 12:39:13

您可以使用资源参数.如果您没有在路径中指定占位符,它们将自动转换为查询字符串参数.像这样:

You can use resource parameters. If you haven't specified placeholders in the path, they would automatically be converted into query string params. Like that:

angular
    .module('myServices', ['ng', 'ngResource'])
    .factory('Item', [
         '$resource',
         function ($resource) {
             return $resource('/api');
     }]);

Item.query({p: 'item/1'});

这将导致对 /api?p=item/1 的请求.

This would result in a request to /api?p=item/1.

附言

我想你已经知道了,但你不喜欢它.但我仍然认为这是您的情况的正确方法.考虑到您正在处理该后端的糟糕 API 设计,您可以将 AngularJS 资源与另一个为您执行此操作的服务包装在一起.

I suppose you already know that, but you don't like it. But I still think this is the correct way in your case. Considering the bad API design you are dealing with that back-end you could wrap the AngularJS resources with another service which does this for you.