gpt4 book ai didi

javascript - 在 ngResource 中指定 url 参数

转载 作者:行者123 更新时间:2023-11-30 15:39:46 25 4
gpt4 key购买 nike

我有一个使用 ngResource 的服务,我用它来访问我的网络应用程序中特定新闻帖子的评论。

我的问题是,当我想查询特定新闻帖子的评论时,就像这样 article.comments = commentService.query()get 请求发送到 /api/news/comments,而不是 /api/news/:id/comments。如何指定 :id 以便将 get 请求发送到正确的 url(/api/news/:id/comments)?

评论服务

   .factory('commentService', function($resource){
return $resource('/api/news/:id/comments/:comment', {id: '@news', comment: '@comment'}, {
update: {
method: 'PUT'
}
}, {
stripTrailingSlashes: false
}
)})

在 ng-click 上获取评论的方法

$scope.getComments = function(article) {
article.comments = commentService.query()
}

最佳答案

我是这样解决的

$scope.getComments = function(article) {
return commentService.query({id:article._id})
}

关于javascript - 在 ngResource 中指定 url 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40995404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com