[英]AngularJS: POSTing something new with $resource
我在使用$ resource發布新內容時遇到了麻煩...它非常適合更新和查詢,但是當我嘗試創建新的“文章”時,它會變得有些混亂。
基本上,我的POST應該發送到“ / articles /”。 文章的ID是通過前端的表單設置的,但是,當我提交POST時,它會發送到“ / articles / ARTICLE_ID”(ARTICLE_ID被替換為我在表單上設置的任何內容)。
這是我的文章服務:
angular.module('testapp.articles')
.factory("Articles", ['$resource', function($resource) {
return $resource('articles/:articleId', {
articleId: '@articleId'
}, {
update: {
method: 'PUT'
}
});
}]);
這是我的控制器:
$scope.create = function() {
var article = new Articles({
articleId: this.articleId
});
article.$save(function(response){
console.log(response);
});
};
任何幫助表示贊賞,謝謝!
它轉到/articles/ARTICLE_ID
因為這是您在創建Article
指定的內容。 您無需提供article_id
作為參數。
$scope.create = function() {
var article = new Articles();
article.articleId = this.articleId
article.$save();
...
};
對於較新版本的AngularJS,無論如何都會選擇articleId
。 您的服務可以簡化如下:
angular.module('testapp.articles')
.factory("Articles", ['$resource', function($resource) {
return $resource('articles/:articleId', null,
{
'update': { method: 'PUT' }
});
}]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.