http://stackoverflow.com/questions/18374357/nested-promises-not-resolving-until-a-button-is-clicked
Angular doesn't magically watch all timeouts. It needs a hook to be told that something might have changed, which triggers a $digest
loop.
You can do it manually, by calling $scope.$apply()
, or by using inbuilt handlers (like ng-click
). But for this use case there's also the $timeout
service which you can use in place of setTimeout
.
See API Reference/ng/$timeout.
现象:调用$q.all(),但一直没返回,直到下一次的$digest
比如
var modalInstance = $modal.open({ templateUrl: 'ConfirmModalContent.html', controller : 'ConfirmModalController', backdrop : true, resolve : { opts : function () { return angular.extend(opts, {showCancel: true}); } } }); modalInstance.result.then(function (result) { if (result === 'ok') { cbfn(); } })
当中ui-bootstrap的$modal.open()方法中调用了$q.all(),有时候却没有触发
办法,如下
var modalInstance; $timeout(function () { modalInstance = $modal.open({ templateUrl: 'ConfirmModalContent.html', controller : 'ConfirmModalController', backdrop : false, resolve : { opts : function () { return opts; } } }); if (cbfn) { modalInstance.result.then(function (result) { cbfn(); }, function () { cnfn(); }) } }, 0);
解释一下:加入$timeout(),使angular知道,将有事情发生,盯紧咯
一切ok