angularjs – 传递给$resource的@id是什么?

前端之家收集整理的这篇文章主要介绍了angularjs – 传递给$resource的@id是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. $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 postDataobject if
non-GET “class” action is used,or the instance itself if non-GET
instance action is used.

如果我正确理解这一点,而我可能不会,参数{id:@id}是另一种方式为您的url变量提供一段数据的例证。

给定这个方法

  1. var myResource = $resource("/posts/:theName",{theName: '@petName'},{enter : {
  2. method: "POST",isArray: false
  3. }
  4. });

如果我在我发布的数据中有属性“petName”,该属性的值将放置在:url中的theName变量。假设发布数据为{“petType”:“cat”,“petName”:“Spot”},网址将显示为“/ posts / Spot”。在我看来,@表示要发布的对象的“属性”。

从该值中取出@,url变量将直接引用该资源参数中的值:

  1. {theName: 'petName'} //no "@"
  2. // url output ----> '/posts/petName'

这里是参考链:

  1. //url var--> //$resource param {..} --->//Object to be posted
  2. :theName---> {theName ----> @petName ---> {petName---> "Spot"

它只需要5个步骤,以获得“Spot”进入网址!

使用上面示例的资源实例示例:

  1. var postData = new myResource();
  2. postData.petType = "cat";
  3. postData.petName = "Spot";
  4. postData.$enter({},function(data){
  5. $scope.data = data;
  6. })
  7. // url to post to will be '/posts/Spot',postData object will be
  8. // {"petType":"cat","petName:"Spot"}

从旁边来看,文档可能很混乱。你有没有走过一个困难的课程,教授是一个辉煌的人,几乎不说你的语言?对。

猜你在找的Angularjs相关文章