Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
918 views
in Technique[技术] by (71.8m points)

angularjs - "at" sign in parameter names in resource definition

from the documentation (http://docs.angularjs.org/api/ngResource.$resource):

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

lets say you have a resource like this:

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

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.

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.

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() { .. })

will produce the request: GET /user/123/Matt


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...