传递参数所需的语法会破坏成功回调。答案是参数。当不使用额外的块查询时,我可以获得成功回调:function(answer){
Answer.query(answer, function(data) {
console.log(data);
});
这是服务
.factory('Answer',function($resource, $cookies) {
return {
query: function(answer) {
return $resource('/api/answers/', {'question_id': $cookies.question_id, 'answer': 'test'},
{
query: { method: 'POST', isArray: false}
}).query();
}
} })
这是答案。谢谢Gordon Bockus和Grundy。这为我节省了很多时间。
Answer.query(回答);
.factory('Answer',function($resource, $cookies) {
return {
query: function(answer) {
return $resource('/api/answers/', {'question_id': $cookies.question_id, 'answer': answer},
{
query: { method: 'POST', isArray: false}
}).query(function(result) {
console.log(result);
});
}
}
})
答案 0 :(得分:1)
您将参数传递给工厂查询函数而不是资源查询函数。如果您想保持相同的结构,则需要更新工厂
.factory('Answer',function($resource, $cookies) {
return {
query: function(answer, callback) {
var result;
return result = $resource('/api/answers/', {'question_id': $cookies.question_id, 'answer': 'test'},
{
query: { method: 'POST', isArray: false}
}).query(function() {
callback(result);
});
}
查看此页面上的示例信用卡资源https://docs.angularjs.org/api/ngResource/service/ $ resource