我使用以下代码进行使用$ ionicPopup和Firebase的一些验证:
onTap: function(e) {
firebase.auth().applyActionCode($scope.data.emailcnfrm)
.then(function() {
return $scope.data.emailcnfrm;
},
function(error) {
alert("wrong code");
e.preventDefault();
});
}
但是如果出现错误,并且在我收到“错误代码”警告后,弹出窗口将被关闭,因为e.preventDefault();
已被忽略。
那么我的代码究竟出了什么问题呢?我该如何解决这个问题?
答案 0 :(得分:1)
您对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
});
});
答案 1 :(得分:1)
最后,我用自己的技巧解决了这个问题:
-outside Async:
hashCode()
-inside async:
$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
});
});