我尝试使用angular delete
资源,这会在Chrome控制台中产生SyntaxError
。但是,会创建正确的服务器请求,并且会在服务器上删除资源。
脚本:
var libmsModule = angular.module('libmsModule', ['ngResource']);
libmsModule.factory('Book', function($resource) {
return $resource('/libms/books/:id', {id: '@id'}, {
update: {
method: 'PUT'
}
});
});
libmsModule.controller('BookCtrl', function($scope, Book) {
$scope.deleteBook = function(index) {
var bk = $scope.books[index];
$scope.book = new Book();
$scope.book.id = bk.id;
$scope.book.$delete(function() {
//console.log("deleting... " + index);
//$scope.books.splice(index, 1);
});
}
$scope.books = Book.query(function(data) {
});
});
HTML:
<div id="home-container" class="home" data-ng-app="libmsModule">
<div data-ng-controller="BookCtrl">
<a href="#" data-ng-click="addBook()">Add Book</a>
<ul>
<li data-ng-repeat="book in books" data-ng-click="deleteBook($index)">{{book.id}} {{book.author}} {{book.title}} {{book.cost}}</li>
</ul>
</div>
</div>
这导致:
Request URL:http://localhost:8780/libms/books/44
Request Method:DELETE
服务器(Spring MVC):
@RequestMapping(value = { "/books/{id}" }, method = RequestMethod.DELETE)
public ResponseEntity<String> deleteBook(@PathVariable("id") Integer id,
Model model) throws Exception {
logger.entry();
bookSvc.deleteBook(new Book(id));
logger.exit();
return new ResponseEntity("Book deleted", HttpStatus.OK);
}
相应地删除了本书,但控制台中显示以下内容:
响应标签(chrome控制台)显示:Book deleted
。
我是否需要将delete
的内容添加到Book Factory
?