使用多个可选参数的角1.0.8 $资源。

时间:2023-01-21 19:44:10

My Student ulr looks like this:

我的学生ulr是这样的:

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

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

当我没有参数调用它时,我希望url是/app/student/(工作)

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

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

当我调用studentid时,我希望url是/app/student/?id = 88(工作)

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

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

当我用课程id打电话时,我想要的url是/app/学生/?courseid = 99(不)

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

Instead I get: /app/student/6682831673622528

而不是我:/ app /学生/ 6682831673622528

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

当我与学生和课程id通话时,我想:/app/学生/?id=1,而class =2,我得到/应用/学生/12

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

为了url: /app/学生/:studentid&id提供给我/app/学生/1&2,尝试这样的东西

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

不知何故,只提供学生证明是有效的,但课程id或两者都不是我想要的。我不确定我将如何期望它,因为文档中没有关于多个参数作为查询字符串的任何内容(有关于扩展url的/app/student/student /studentid/1/courseid/2,但由于这是一个xhr请求,我希望它请求/app/student/,并将GET参数附加为? 1 =val&two=val

Is there a way to do this?

有没有办法做到这一点?

1 个解决方案

#1


37  

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.

其他的案例也会起作用。不要将路径参数与查询参数混淆。

#1


37  

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.

其他的案例也会起作用。不要将路径参数与查询参数混淆。