有多个可选GET参数角1.0.8 $资源 [英] Angular 1.0.8 $resource with multiple optional get parameters

查看:108
本文介绍了有多个可选GET参数角1.0.8 $资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的学生ULR看起来是这样的:

My Student ulr looks like this:

var Student = $resource('/app/student/:studentid:courseId',
    {studentid:'@id',courseId:'@cid'}
);

当我把它称为不带参数,我想URL中 /应用/学生/ (作品)

When I call it without parameters I would like the url be /app/student/ (works)

var names=Student.query(
  function(){
      deferred.resolve(names);
  }
);

当我studentid调用它,我想的网址是 /应用/学生/?ID = 88 (作品)

When I call it with studentid I would like the url be /app/student/?id=88 (works)

    Student.get({"id":id},
      function(data){
          deferred.resolve(data);
      }
    );

当我打电话courseid只有我想的网址是 /应用/学生/?courseid = 99 (犯规)

When I call with courseid only I would like the url be /app/student/?courseid=99 (doesnt)

    Student.query({courseId:cId},
      function(data){
          deferred.resolve(data);
      }
    );

相反,我得到: /应用/学生/ 6682831673622528

当我与学生和课程ID叫我想:<?code> /应用/学生/ ID = 1&安培; courseid = 2
相反,我得到 /应用/学生/ 12

When I call with both student and course id I would like: /app/student/?id=1&courseid=2 Instead I get /app/student/12

试图像这样的网址: /应用/学生/:studentid&放大器;:courseid 给我 /应用/学生/ 1和2

Trying something like this for the url: /app/student/:studentid&:courseid gives me /app/student/1&2

不知何故,只给studentid的作品,但courseid或两个不工作,我想它。不知道我怎么会想到它,因为没有什么关于多个参数作为查询字符串(没有与延长URL的文件在/ app /学生/ studentid / 1 / courseid / 2 但因为它是一个XHR请求,我想拥有它要求 /应用/学生/ 与追加为?一种= VAL&安培;二= VAL

Somehow, only giving studentid works but courseid or both doesn't work as I would want it. Not sure how I would expect it because there nothing in the documentation about multiple parameters as a query string (there is on extending the url with /app/student/studentid/1/courseid/2 but since it's an xhr request I'd like to have it request /app/student/ with the GET parameters appended as ?one=val&two=val

有没有办法做到这一点?

Is there a way to do this?

推荐答案

如果你需要的参数映射到查询参数,而不是路径参数,你不应该把参数的名字进入的路径。例如。学生资源应该是这样的:

If you need parameters to be mapped to the query parameters, not the path parameters, you shouldn't put the name of parameters into the path. E.g. student resource should look like this:

var Student = $resource('/app/student/',
    {}
);

然后像这样调用将没有任何问题的工作:

Then invocation like this will work without any problems:

 Student.get({id:12,courseId:55});//calls /app/student/?id=12&courseId=55
 Student.get({id:12});//calls /app/student/?id=12
 Student.get({courseId:55});//calls /app/student/?courseId=55

其他人的情况下将正常工作。只是不要混淆路径参数与查询参数。

The others cases will work as well. Just don't confuse path parameters with query parameters.

这篇关于有多个可选GET参数角1.0.8 $资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆