- $resource("/entries/:id",{id: "@id"},{update: {method: "PUT"}})
什么是@id?
在$ resource doc page有人这样说下,但我还是不明白。
@H_301_7@If the parameter value is prefixed with @ then the value of that
parameter is extracted from the data object (useful for non-GET
operations).” The data object here refers to the postData
object if
non-GET “class” action is used,or the instance itself if non-GET
instance action is used.
如果我正确理解这一点,而我可能不会,参数{id:@id}是另一种方式为您的url变量提供一段数据的例证。
给定这个方法:
- var myResource = $resource("/posts/:theName",{theName: '@petName'},{enter : {
- method: "POST",isArray: false
- }
- });
如果我在我发布的数据中有属性“petName”,该属性的值将放置在:url中的theName变量。假设发布数据为{“petType”:“cat”,“petName”:“Spot”},网址将显示为“/ posts / Spot”。在我看来,@表示要发布的对象的“属性”。
从该值中取出@,url变量将直接引用该资源参数中的值:
- {theName: 'petName'} //no "@"
- // url output ----> '/posts/petName'
。
这里是参考链:
- //url var--> //$resource param {..} --->//Object to be posted
- :theName---> {theName ----> @petName ---> {petName---> "Spot"
它只需要5个步骤,以获得“Spot”进入网址!
。
使用上面示例的资源实例示例:
- var postData = new myResource();
- postData.petType = "cat";
- postData.petName = "Spot";
- postData.$enter({},function(data){
- $scope.data = data;
- })
- // url to post to will be '/posts/Spot',postData object will be
- // {"petType":"cat","petName:"Spot"}
从旁边来看,文档可能很混乱。你有没有走过一个困难的课程,教授是一个辉煌的人,几乎不说你的语言?对。