AngularJS - 双向绑定承诺 - 处理拒绝

时间:2013-04-19 15:50:55

标签: angularjs promise

我已经找到了当你双向绑定一个promise时很难的方式,angular会解析你的承诺,并且会绑定实际的承诺。我的问题是,如何处理被拒绝的承诺?

我的特殊情况是我有一个指令,我从控制器双向绑定 promise 。我的控制器期望该指令处理对该promise的拒绝,因为错误需要显示在DOM上。

在我的指令中,我希望我的绑定变量是一个promise,但我获得了该promise的已解析值。很讨厌,但并不可怕。

问题是当该承诺被拒绝时,指令无法知道

以羽毛球为例:

http://plnkr.co/edit/m0cOqFhx6TNrDxTbr9Qx?p=preview

我如何处理指令中的承诺拒绝?

谢谢, 罗伊

2 个答案:

答案 0 :(得分:1)

我有三个建议 - 没有一个是完美的,但它们都有效:

  1. 创建一个getter方法,该方法返回promise并使用&

  2. 将此方法绑定到指令
  3. 绑定延迟对象而不是promise(丑陋)

  4. 在承诺解决/被拒绝时广播事件

  5. 我创建了一个包含所有建议的plunker:http://plnkr.co/edit/jsA0PwpQm0xycLclkBU3?p=preview

    • 控制器:
    app.controller('MainCtrl', function($scope,$q,$timeout) {
      var deferred = $q.defer();
    
      $scope.promise = deferred.promise;
      $scope.deferred = deferred;
    
      $scope.getPromise = function(){
        return deferred.promise;
      }
    
      $scope.promise.then(function(msg){
        $scope.$broadcast('promiseThen',{rejected:false,msg:msg});
      },function(msg){
        $scope.$broadcast('promiseThen',{rejected:true,msg:msg});
      });
    
      $timeout(function(){deferred.reject('No reason...');},1500)
    });
    
    • 标记:
    <div promise-test deferred="deferred" get-promise='getPromise()' ></div>
    
    • 指令:
    app.directive('promiseTest',function(){
      return {
        template:'<div>m1: {{m1}}<br>m2: {{m2}}<br>m3: {{m3}}</div>',
        scope:{getPromise:'&',deferred:'='}
        ,link:function(scope,el,attrs){
    
          scope.m1 = scope.m2 = scope.m3 = 'Waiting...';
    
          scope.getPromise().then(function(msg){
            scope.m1 = 'Resolve getPromise: '+msg;
          },function(msg){
            scope.m1 = 'Reject getPromise: '+msg;
          })
    
          scope.deferred.promise.then(function(msg){
            scope.m2 = 'Resolved deferred.promise: '+msg
          },function(msg){
            scope.m2 = 'Rejected deferred.promise: '+msg
          });
    
          scope.$on('promiseThen',function(ev,val){
            if(val.rejected){
              scope.m3 = 'Rejected promiseThen: '+val.msg
            }else{
              scope.m3 = 'Resolved promiseThen: '+val.msg          
            }
          })
        }
      }
    })
    

答案 1 :(得分:1)

成功和错误回调可以使用&作为参数传递。

在此选项中,控制器负责将回调添加到.then

控制器

app.controller('MainCtrl', function($scope,$q,$timeout) {
  var deferred = $q.defer();

  $scope.register = function(callback, errback) {
    deferred.promise.then(callback, errback);
  }

  $timeout(function(){deferred.reject('No reason...');},1500)
});

指令

app.directive('promiseTest',function(){
  return {
    template:'<div>m1: {{m1}}',
    scope:{register:'&'},
    link:function(scope,el,attrs){

      scope.m1 = 'Waiting...';

      scope.register({
        success: function(msg){
          scope.m1 = 'Resolve getPromise: '+msg;
        },
        errback: function(msg){
          scope.m1 = 'Reject getPromise: '+msg;
        }
      });
    }
  }
})

HTML

<div promise-test register='register(success, errback)' ></div>

plunkr