简体   繁体   English

AngularJS 如何处理异步调用

[英]AngularJS how to handle async calls

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();
});

When I add/delete item(s) and read the todo list from server again to refresh it.当我添加/删除项目并再次从服务器读取待办事项列表以刷新它时。 I find that I will hit this error from time to time:我发现我时不时会遇到这个错误:

System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.

Someone said I can put MultipleActiveResultSets=true;有人说我可以把MultipleActiveResultSets=true; into connect string, but I suspect it will only hide my issue, because the GET is obviously trying to have a dirty read before DELETE can finish its job.进入连接字符串,但我怀疑它只会隐藏我的问题,因为 GET 显然试图在 DELETE 完成其工作之前进行脏读。

In event based style language, I will naturally call GET after a DELETEComplete() event.在基于事件的样式语言中,我自然会在 DELETEComplete() 事件之后调用 GET。 However, I am new to AngularJS, I am not sure how it is done here?但是,我是 AngularJS 的新手,我不确定这里是如何完成的? Please help.请帮忙。

I am not a expert, but I would place the read code in the success callback of the save.我不是专家,但我会将读取的代码放在保存的成功回调中。 Something like this:像这样的东西:

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();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM