【发布时间】:2014-02-19 21:49:35
【问题描述】:
我有以下用于论坛的基本 API:
-
POST /topics(创建新主题) -
GET /topics(获取所有主题) -
GET /topics/1(获取 ID 为“1”的主题)
我想添加以下内容:
-
POST /topics/1(添加对 ID 为“1”的主题的回复)
我尝试了以下代码(相关摘录),但没有奏效:
.controller('TopicReplyController', function ($scope, $routeParams, Topics) {
'use strict';
var topicId = Number($routeParams.topicId);
Topics.get({topicId: topicId}, function (res) {
$scope.topic = res;
});
$scope.postReply = function () {
var newPost = new Topics({
topicId: topicId
});
newPost.text = $scope.postText;
newPost.$save(); // Should post to /topics/whatever, not just /topics
};
})
.factory('Topics', function ($resource) {
'use strict';
return $resource('/topics/:topicId', {topicId: '@id'});
});
它只是向/topics发出请求,这是行不通的。
有什么想法可以让它发挥作用吗?
【问题讨论】:
-
您要更新 /topics/1 还是创建一个 ID 为 1 的新“主题”?
-
我想向 ID 为 1 的主题添加新帖子,这需要向 /topics/1 发送 POST 请求。这很不寻常,但它仍然是 RESTful。
标签: angularjs rest angular-resource ngresource