如何使用 Angular $resource 传递查询参数数组

How to pass query parameter array using Angular $resource

我正在尝试启用一项功能,可以使用以下结构根据多个标签(标签数组)搜索用户:

GET '/tags/users?tag[]=test&tag[]=sample'

我在我的节点服务器上运行它,并使用 Postman 成功测试了它。我 运行 遇到的问题是如何使用 $resource 在我的 Angular 服务中构建此 GET 请求。我找到了 $http 的文档,说明将 params:{'tag[]': tag} 添加到请求对象就可以了,但是找不到任何关于 $resource.

的信息

在 angular 中,您可以使用 $resource

将数组作为查询字符串传递

在控制器中:

    angular.module('myApp').controller('userController',['$scope','userService', '$location',
        function($scope, userService, $location){
            $scope.searchQuery = {};

            $scope.SearchUsers = function() {
                $scope.roles = ['admin', 'register', 'authenticate'];
                $scope.searchQuery.name ='xxx';
                $scope.searchQuery['roles[]'] = $scope.roles;
                userService.searchUsers($scope.searchQuery)
                    .$promise.then(function (response) {
                        $scope.users = response.users;
                    });
            };
       }
   ]);

服役中:

    angular.module('myApp').factory('userService', ['$resource',
  function($resource) {

    return {
        searchUsers: function(searchQuery){
            var searchRequest = $resource('/auth/search/user', {}, {
                'get': {method: 'GET'}
            });
            return searchRequest.get(searchQuery);
        }
    };
  }
]);

请求url喜欢:

/auth/search/users?name=xxx&roles%5B%5D=admin&roles%5B%5D=register&roles%5B%5D=authenticate

您在请求中得到 %5B%5D 而不是 [] url

如果您期望 return array 而不是 object 那么您应该使用

'get': {method: 'GET', isArray: true}