$ionicPopup: e.preventDefault() 被忽略

$ionicPopup: e.preventDefault() gets ignored

我将以下代码用于一些同时使用 $ionicPopup 和 Firebase 的验证内容:

onTap: function(e) {
    firebase.auth().applyActionCode($scope.data.emailcnfrm)
        .then(function() {
            return $scope.data.emailcnfrm;
        },
        function(error) {   
            alert("wrong code");
            e.preventDefault();
        });
}

但是在出现错误的情况下,在我收到 "wrong code" 警报后,弹出窗口将关闭,因为 e.preventDefault(); 已被忽略。

那么我的代码究竟有什么问题呢?我该如何解决这个问题?

你对firebase.auth().applyActionCode的调用是异步的,e.preventDefault会在错误回调中被异步调用。这种情况发生在 之后 用户已经调用 onTap,因此 e.preventDefault 不会有任何效果。

编辑(建议修复)

要修复它,您可以将异步逻辑与 onTap 方法分开:

var myPopup = $ionicPopup.show({
  onTap: function(e) {
    // always keep the popup open, because we'll decide when to close it later
    e.preventDefault();
  }
});

myPopup.then(function(res) {
  firebase.auth().applyActionCode($scope.data.emailcnfrm)
  .then(function() {
    // if successful, imperatively close the popup
    myPopup.close();
  }, function(err) {
    // log the error, or something
  });
});

最后我用自己的绝招解决了这个问题:

-外部异步:

         $scope.myPopup = $ionicPopup.show({

             scope: $scope, buttons: [

             { text: 'Cancel' },

             {                  
                 text: '<b>Done</b>',

                 type: 'button-positive',

                 onTap: function(e)
                 {

                 // always keep the popup open, because we'll decide when to close it later
                 e.preventDefault();

                 $AsyncCallback();
                 }
             }
             ]
         });

$scope.myPopup.then(function(){},
                             function(error)
                             {
                                 $ionicPopup.alert({

                                 title: 'Internal error!',

                                 template: error
                                 });
                             });

-内部异步:

$scope.myPopup.close();