AngularJS承诺链

ras*_*cio 5 promise chain angularjs

我的应用程序应该打开弹出窗口向用户询问确认,然后创建ajax cal并关闭弹出窗口.
我尝试使用一系列承诺(我已经使用它,我记得它应该以这种方式工作),但它似乎在调用后阻止reservationService.confirm($scope.object);.现在它是一个用a实现的虚假服务setTimeout,$q只是为了返回一个promise(将来它将进行ajax调用).这是一个有效的代码还是我没有发现承诺是如何工作的?
对于弹出窗口我选择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});
        });
Run Code Online (Sandbox Code Playgroud)

reservationService:

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

setTimeout使用$timeout角度服务解决了 变化

ras*_*cio 6

使用$timeout而不是setTimeout'因为它在角度范围内工作,强制digest相位(或$scope.apply()在内部使用setTimeout).