[英]How can I pass a parameter into a resource without changing the route?
Basically, I need to get one parameter from my angular app to be used in my nodeJS service. 基本上,我需要从我的angular应用程序中获取一个参数,以便在nodeJS服务中使用。 I have a GET request that looks like this:
我有一个类似的GET请求:
resource: $resource(nodeRoute + '/test'. {}, {
query: {
method: 'GET',
params: {
offset: '@offset',
limit: '@limit'
},
isArray: true,
url: ohio.resources.netreg + '/registrations'
},
}
Where 'offset' and 'limit' are what I am trying to pass in, via this call in my routeprovider's resolve block: 我要在路由提供者的resolve块中通过此调用传递“ offset”和“ limit”:
.when('/'. {
templateUrl: 'views/home.html',
title: 'test',
resolve: {
registrations: ['thisResource', function(thisResource){
return thisResource.resource.query(0, 2).$promise
]);
}
Where I'm trying to pass the values 0 and 2 in for offset and limit, so that I can then go on to use those values in my server side node stuff. 我尝试将0和2的值传递给offset和limit的地方,这样我就可以继续在服务器端节点中使用这些值了。 This works great if, in my resource, I hard code offset and limit to some values, but I need to update these values, thus this needs to work like I'm trying to do above.
如果在我的资源中我硬编码偏移量并将其限制为某些值,则此方法非常有用,但是我需要更新这些值,因此这需要像我在上面尝试做的那样工作。 What am I missing?
我想念什么? I'm assuming the @ character only works if I have a route like /:offset/:limit, but I can't really do that here.
我假设@字符仅在我有// offset /:limit之类的路由时才有效,但是我在这里不能真正做到这一点。
try this 尝试这个
resource: $resource(nodeRoute + '/test/:offset/:limit' {}, {
query: {
method: 'GET',
params: {
offset: '@offset',
limit: '@limit'
},
isArray: true,
url: ohio.resources.netreg + '/registrations'
},
}
and in resolve use this 并决心使用这个
resolve: {
registrations: ['thisResource', function(thisResource){
return thisResource.resource.query({offset:0, limit:2}).$promise
]);
}
If you dont want to specify the parameters via a get, then another option is using a post where you can specify params without necessarily appending to your url. 如果您不想通过get指定参数,那么另一个选择是使用post,您可以在其中指定参数而不必附加到您的url。
so in your node js you can access the post params like 所以在您的节点js中,您可以访问post参数,例如
postrouter.post('/test', function (req, res){
//you can access your post params via req.body
console.log(req.body)//gives you your passed objects
})
then in your resource instead of a get it'll be a post. 然后在您的资源中(而不是获取)它将是一个帖子。 in your controller you can pass your params like this in your resolve.
在您的控制器中,您可以在解析中传递这样的参数。
resolve: {
registrations: ['thisResource', function(thisResource){
return thisResource.resource.query({offset:0, limit:2}).$promise
]);
}
hope it helps.!! 希望能帮助到你。!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.