【发布时间】:2015-06-04 21:49:21
【问题描述】:
我想通过 angular ngResource 使用 post 方法来持久化数据。 我已经为该过程编写了以下代码。但是,当我试图坚持 错误来了。
JS 代码
angular.module('app').factory("testService", function($resource) {
return{
addCount: $resource('/save/count',{method:'POST',params:{ date: '@date',
count: '@count'}}
)
};
});
//控制器代码
$scope.add = function(){
testService.addCount().get({ date: new Date(formattedDate).toString("MM/dd/yyyy"),
count: count},function(response){
console.log('success')
});};
一旦我从我的 html 中单击 add(),我就会收到以下错误。 TypeError:无法读取未定义的属性“get”
请提供解决此问题的建议
【问题讨论】:
-
您是否在控制器中正确注入服务?
-
试试 testService.addCount.get(....
-
是的,我注射了@chris
-
您没有正确使用 $resource。
'save': {method:'POST'},根据文档,如果要发布,应使用方法save -
还有
testService.addCount()不要把它作为函数调用,返回一个对象
标签: javascript angularjs