" AT"登录在资源定义的参数名称 [英] "at" sign in parameter names in resource definition

查看:156
本文介绍了" AT"登录在资源定义的参数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从文件(<一个href=\"http://docs.angularjs.org/api/ngResource.%24resource\">http://docs.angularjs.org/api/ngResource.$resource):

$resource(url[, paramDefaults][, actions]);

paramDefaults(可选) - {对象=} - 为URL参数的默认值。
...
如果参数值是与 pfixed @ $ P $然后该参数的值是从数据对象中提取

paramDefaults(optional) – {Object=} – Default values for url parameters. ... If the parameter value is prefixed with @ then the value of that parameter is extracted from the data object.

问题是,什么样的数据对象,它们是指?如何使用此功能?

The question is what data object do they refer to? How to use this feature?

推荐答案

可以说你有一个这样的资源:

lets say you have a resource like this:

var User = $resource('/user/:userId', {userId:'@id'});
var user = User.get({userId:123});

这意味着的值:用户id 在您的网址将与 ID代替酒店需要从用户要求该属性时反对。

It means that the value of :userId in your url will be replaced with the id property from the user object when that property is required.

因此​​,当需要它?其时,你正在做的事情,以现有的用户,如 GET ING之一,更新一个必需的。当你创建一个用户它不是必需的。

So when is it required? Its required when you are doing something to an existing user, like geting one, updating one. It is not required when you create a user.

在大多数情况下,你会希望至少有一个参数prefixed与@在你的REST URL中使用的资源(可能是对象ID)。如果你不有一个,这意味着,为了让您保存一个对象的实例,你不需要知道它的存储位置事情。这意味着,它的一个单独的对象。也许就像一个设置对象。

In most cases, you will want to have at least one param prefixed with @ in your REST url that resource uses (probably the object id). If you dont have one, that means that in order for you to save an instance of an object, you dont need to know anything about where its stored. This implies that its a singleton object. Maybe like a settings object.

下面是您期待已久的例子:

Here is your long awaited example:

var User = $resource('/user/:userId/:dogName', {userId:'@id', dogName:@dog});
User.get({userId:123, dog:'Matt'}, function() { .. })

会产生请求:GET /用户/ 123 /马特

这篇关于&QUOT; AT&QUOT;登录在资源定义的参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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