1

我正在使用平均堆栈。我正在尝试将 PUT 请求添加到我的 $ngResource 中,但出现错误:

TypeError: conService.update is not a function

at m.$scope.joinCon (http://localhost:3000/javascripts/chirpApp-ngresource.js:217:14)

at lb.functionCall (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:200:64)

at Hc.(anonymous function).compile.d.on.f (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:216:394)

at m.$get.m.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:126:250)

at m.$get.m.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:126:476)

at HTMLButtonElement.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:216:446)

at HTMLButtonElement.c (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:32:389)

我的代码

//My factory
app.factory('conService', function ($resource) {return $resource('/api/cons', {'update': { method: 'PUT' }});});

//My update
conService.update({conName:$scope.cons.conName},$scope.cons);};

//My api to get all conventions
.get(function (req, res) {Con.find(function (err, cons) {if (err) { return res.send(500, err); }return res.send(cons);});})

谁能帮我弄清楚我在更新中遗漏了什么?谢谢

4

1 回答 1

0

由于它是一个工厂,因此您应该在使用控制器代码之前对其进行实例化,如下所示:

var cs = new conService();
cs.update(/*params here*/);

有关更多详细信息,请阅读那些精彩的帖子:AngularJS:服务与提供者与工厂AngularJS:工厂和服务?

于 2015-08-08T15:44:24.760 回答