query string in $resource url

后端 未结 2 2036
隐瞒了意图╮
隐瞒了意图╮ 2020-12-25 11:09

my service has to use a query string due to limitations on the server that runs classic ASP:

angular
  .module(\'myServices\', [\'ng\', \'ngResource\'])
  .f         


        
相关标签:
2条回答
  • 2020-12-25 11:26

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

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

    P.S.

    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.

    0 讨论(0)
  • 2020-12-25 11:44
    var deferred = $q.defer();
    api.api_name.query({
        'param':param_value
    },
        function(response) {
            deferred.resolve(response);
        },
        function(response) {
            deferred.reject(response);
        }
    );
    //
    angular
        .module('module_name')
        .factory('api',function($resource){
            var api_var={};
            api_var.api_name = $resource('url?param_key=:param', {
                param: '@param'
            }, {
                'query': {
                    method: 'get'
                }
            });
            return api_var;
        });
    
    0 讨论(0)
提交回复
热议问题