var todoApp = angular.module('TodoApp', ['ngResource']);
todoApp.value('restTodo', 'api/1/todo/:id');
var todoCtrl = todoApp.controller('TodoCtrl', function($scope, $resource, restTodo) {
$scope.src = $resource(restTodo);
//add & refresh
$scope.src.save({ content: 'hello world', done: false });
$scope.todos = $scope.src.get();
//OR
//delete & refresh
$scope.src.delete({ id: '1' });
$scope.todos = $scope.src.get();
});
当我添加/删除项目并再次从服务器读取待办事项列表以刷新它时。我发现我时不时会遇到这个错误:
System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.
有人说我可以将 MultipleActiveResultSets=true;
放入连接字符串中,但我怀疑这只会隐藏我的问题,因为 GET 显然试图在 DELETE 完成其工作之前进行脏读.
在基于事件的风格语言中,我自然会在 DELETEComplete() 事件之后调用 GET。但是,我是 AngularJS 的新手,我不确定这里是如何完成的?请帮忙。
最佳答案
我不是专家,但我会将读取的代码放在保存的成功回调中。像这样的事情:
var todoApp = angular.module('TodoApp', ['ngResource']);
todoApp.value('restTodo', 'api/1/todo/:id');
var todoCtrl = todoApp.controller('TodoCtrl', function($scope, $resource, restTodo) {
$scope.src = $resource(restTodo);
//add & refresh
$scope.src.save({ content: 'hello world', done: false },
function(data) {
$scope.todos = $scope.src.get();
});
});
关于javascript - AngularJS 如何处理异步调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13304131/