简体   繁体   English

等待多个单独的承诺解决

[英]Wait for multiple separate promises to resolve

i have angular promises across different controllers, and i want to execute function once all of them have been finished. 我在不同的控制器上都有承诺,并且我想在所有控制器完成后执行功能。

Is there any way like via events or promises to accomplish this? 是否可以通过事件或诺言来完成此任务?

Take a look at the documentation for $q , in particular $q.all() . 查看$q的文档,尤其是$q.all()

https://docs.angularjs.org/api/ng/service/ $q https://docs.angularjs.org/api/ng/service/ $ q

Angular documentation makes it very clear how to share data and methods across controllers.... services (and factories) Angular文档非常清楚地说明了如何在控制器之间共享数据和方法。... 服务 (和工厂)

If you find yourself needing to coordinate multiple controllers, it's an indication that you need a service. 如果您发现需要协调多个控制器,则表明您需要服务。 Services are there to help with exactly this type of situation (and more). 可以提供服务来解决此类情况(以及更多)。

Take a look at this Plunkr: http://plnkr.co/edit/PXVL8YA3lOk7beSvVe5E?p=preview and be sure to open the console so you can see the log messages. 看看这个Plunkr: http ://plnkr.co/edit/PXVL8YA3lOk7beSvVe5E?p=preview并确保打开控制台,以便您可以看到日志消息。

Method D of the service will not execute until Methods AC are resolved. 在解决方法AC之前,服务的方法D将不会执行。

MyService.$inject = ['$q', '$timeout'];
function MyService($q, $timeout) {
  var MyService = this;

  var deferredA = $q.defer();
  var deferredB = $q.defer();
  var deferredC = $q.defer();

  // go ahead and call method D right away
  // it won't fire until all the other methods have completed
  methodD(); 

  MyService.methodA = function() {
    $timeout(function() {
      deferredA.resolve();
      console.log("Method A end!");
    }, 1000);
    console.log("Method A begin!");
  };

  MyService.methodB = function() {
    $timeout(function() {
      deferredB.resolve();
      console.log("Method B end!");
    }, 1000);
    console.log("Method B begin!");
  };

  MyService.methodC = function() {
   $timeout(function() {
      deferredC.resolve();
      console.log("Method C end!");
    }, 1000);
    console.log("Method C begin!");
  };

  function methodD() {
    $q.all([deferredA.promise, deferredB.promise, deferredC.promise]).then(function() {
      console.log("Method D called!");

      // reset all the promises and call method D again
      deferredA = $q.defer();
      deferredB = $q.defer();
      deferredC = $q.defer();
      methodD();
    });
  }
}

Each controller can call the service at any point in time. 每个控制器可以在任何时间点调用服务。 Method D will only execute once all controllers have called in, and their respective promises have been resolved. 方法D仅在所有控制器都已调用并且它们各自的承诺已解决后才执行。

  Ctrl1.$inject = ['$scope', 'MyService'];
  function Ctrl1($scope, MyService) {
    $scope.go = function() {
      MyService.methodA();
    };
  }

  Ctrl2.$inject = ['$scope', 'MyService'];
  function Ctrl2($scope, MyService) {
    $scope.go = function() {
      MyService.methodB();
    };
  }

  Ctrl3.$inject = ['$scope', 'MyService'];
  function Ctrl3($scope, MyService) {
    $scope.go = function() {
      MyService.methodC();
    };
  }

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

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