angularjs – 角度查询中的嵌套参数
发布时间:2020-12-17 08:52:09 所属栏目:安全 来源:网络整理
导读:我有一个基本的角度资源(角度1.0.7): app.factory "User",[ "$resource",($resource)- $resource "/users/:id",{ id: '@id' },{ index: { method: 'GET',isArray: false } }] 我可以传递如下参数: User.index({ foo: 1,bar: 2 }) 但我需要传递嵌套参数: U
我有一个基本的角度资源(角度1.0.7):
app.factory "User",[ "$resource",($resource)-> $resource "/users/:id",{ id: '@id' },{ index: { method: 'GET',isArray: false } } ] 我可以传递如下参数: User.index({ foo: 1,bar: 2 }) 但我需要传递嵌套参数: User.index({ foo: { bar: 1 } }) 这失败了因为它发送: /users?foo=%5Bobject+Object%5D 我试过了: User.index({ foo: JSON.stringify({ bar: 1 }) }) 但显然在服务器端(仅仅是字符串)无法识别参数,我想避免在那里解析的麻烦. 你对这个问题有一个优雅的解决方案吗? 使用jQuery,我已经完成了: $.get("/users",{ foo: { bar: 1 } } ) 生产: /users?foo%5Bbar%5D=1 由服务器完美解释. 似乎like a known issue(here too),让我们从现在开始坚持一个丑陋的补丁……
不像Angular的更改那么优雅,但你应该能够通过设置函数的结果来解决这个问题:
User.index.get({ "foo": (function () { return JSON.stringify({ bar: 1 }); })() } ); 结果类似于您要查找的HTTP请求: ?foo=%7B%22bar%22:1%7D 整个角度的例子: var app = angular.module('myApp',['ngResource']); var restUrl = window.location + 'echo/json/'; app.factory('User',function($resource,$http){ return { index: $resource(restUrl) } }); function RestCtrl($scope,User) { $scope.url = restUrl; User.index.get({ "foo": (function () { return JSON.stringify({ bar: 1 }); })() } ); } http://jsfiddle.net/pherris/U8F8G/6/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |