尝试为TodoApp编写REST API编写Angular,为ADD,UPDATE,GET ALL设置路由,但坚持使用DELETE方法。 我的角度控制器:
angular.module('todoListApp')
.controller('todoCtrl', function($scope, Todo) {
$scope.deleteTodo = function(todo, index) {
$scope.todos.splice(index, 1);
todo.$delete();
console.log("<<" + todo.name + ">> deleted.");
};
角度服务:
angular.module('todoListApp')
.factory('Todo', function($resource){
return $resource('/todos/', {id: '@id'}, {
update: {
method: 'PUT'
},
save: {
method: 'POST'
},
delete: {
method: 'DELETE',
params: {id: '@id'}
}
});
});
我的Spark控制器:
delete("/todos", "application/json", (req, res) -> {
Todo todo = gson.fromJson(req.body(), Todo.class);
if (todo == null) throw new ApiError(404, "Could not find todo.");
todoDao.delete(todo);
return todo;
}, gson::toJson);
delete("/todos/:id", "application/json", (req, res) -> {
int id = Integer.parseInt(req.params("id"));
Todo todo = gson.fromJson(req.body(), Todo.class);
if (todo == null) throw new ApiError(404, "Could not find todo.");
todoDao.delete(todo);
return todo;
}, gson::toJson);
单击DELETE按钮(ng-click =&#34; deleteTodo(todo,$ index)&#34;)第一个控制器被调用,但不是第二个。我做了两个只是为了调试。 错误信息是:
angular.js:10661 DELETE http://localhost:4567/todos?id=3 404 (Not Found)
我可以看到正确的id参数,但为什么控制器没有捕获它?
答案 0 :(得分:0)
答案 1 :(得分:0)
错误在角度.factory代码中,必须有
angular.module('todoListApp')
.factory('Todo', function($resource){
return $resource('/todos/:id', {id: '@id'}, {
update: {
method: 'PUT'
},
save: {
method: 'POST'
},
delete: {
method: 'DELETE',
params: {id: '@id'}
}
});
});
丢失&#39;:id&#39;作为回报$ resourse line。