AngularJS 承诺链

2024-02-12

我的应用程序应该打开一个弹出窗口,要求用户确认,然后进行 ajax cal 并关闭弹出窗口。
我尝试使用一链来做到这一点promise(我已经使用过它,我记得它应该以这种方式工作),但它似乎在调用后阻塞reservationService.confirm($scope.object);。现在它是一个假服务,用setTimeout and $q只是为了返回一个承诺(将来它将进行 ajax 调用)。这是有效的代码还是我不明白 Promise 是如何工作的?
对于弹出窗口,我选择 AngularUI,代码如下:

 reservationService.book($scope.object, day)
        .then(function(){
            var dialogOpts = {/* dialog options omitted*/}
            return $dialog.dialog(dialogOpts).open();

        })
        .then(function(result){
            console.log('confirmed? ', result);
            if (result){
                //After this line it doesn't do nothing, also if the promise is resolved 
                return reservationService.confirm($scope.object);
            }
        })
        .then(function(){
            //this function is never executed
            $scope.$emit('object:detail',{object: $scope.object});
        });

预约服务:

function confirm(){
     var deferred = $q.defer();
     setTimeout(function(){
          console.log('Confirming');
          deferred.resolve(true)
     }, 500);
     return deferred.promise;
}

SOLVED change setTimeout with $timeoutAngular 的服务


Used $timeout代替setTimeout因为它在角度范围内一起工作,迫使digest阶段(或使用$scope.apply()在 - 的里面setTimeout).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AngularJS 承诺链 的相关文章

随机推荐