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

Jam*_*mes 55 angularjs

$resource("/entries/:id", {id: "@id"}, {update: {method: "PUT"}})
Run Code Online (Sandbox Code Playgroud)

什么是@id?

在$ resource doc页面上有人在下面说,但我仍然不明白.

如果参数值以@为前缀,则从数据对象中提取该参数的值(对于非GET操作非常有用)." postData如果使用非GET"class"操作,则此处的数据对象引用该对象,或者如果使用非GET实例操作,则实例本身.

rGi*_*Gil 67

如果我理解正确,而我可能没有,那么该参数{id: @id}说明了为url变量提供一段数据的另一种方法.

鉴于此方法:

var myResource = $resource("/posts/:theName", 
                           {theName: '@petName'},
                           {enter : {
                                      method: "POST", 
                                      isArray: false
                                     }
                            });
Run Code Online (Sandbox Code Playgroud)

如果我在发布的数据中有一个属性"petName",那么该属性的值将放在:theName我的url 中的变量中.想象一下,帖子数据是{"petType": "cat", "petName": "Spot"},网址会读取"/posts/Spot".在我看来,@要发布的对象的意思是"属性".

拿走@从该值和URL变量将直接引用该资源参数的值:

{theName: 'petName'} //no "@"
// url output ---->   '/posts/petName'
Run Code Online (Sandbox Code Playgroud)

.

以下是参考链:

//url var--> //$resource param {..}  --->//Object to be posted
:theName---> {theName ----> @petName ---> {petName---> "Spot"
Run Code Online (Sandbox Code Playgroud)

只需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"}
Run Code Online (Sandbox Code Playgroud)

另外,文档可能非常令人困惑.你有没有选择过艰难的课程而且教授是一个几乎不说你语言的聪明人?对.

  • 整件事情都很模糊.大约有4个不同的地方可以添加url参数.当我编写资源时,我尽可能简单和小巧.任何大的东西,我使用$ http.但那只是我...... (7认同)
  • 最后一部分+1:D.另外,$ scope处理很好 (3认同)