我在使用 $uibModal 并从父窗口的解析中打开另一个模式窗口时遇到了问题,特别是当我使用 setTimeout 延迟打开时,内部窗口似乎在外部窗口解决所有承诺之前不会打开在其解决论点中。
工作示例和非工作示例之间的唯一区别是使用 setTimeout 来延迟内部弹出窗口的打开。
失败示例 - 使用 setTimeout
$scope.doesntWork = function(){
$uibModal.open({
templateUrl : 'popup.html',
resolve : {
test : function(){
var q = $q.defer();
//This inner poppup wont show until the outer one is also
//allowed to show (Resolve is resolved)
setTimeout(function(){
$uibModal.open({
templateUrl : 'popup_inner.html'
});
},1);
setTimeout(function(){
q.resolve(5);
},2000);
return q.promise;
}
}
});
};
工作示例 - 没有 setTimeout
$scope.works = function(){
$uibModal.open({
templateUrl : 'popup.html',
resolve : {
test : function(){
var q = $q.defer();
$uibModal.open({
templateUrl : 'popup_inner.html'
});
setTimeout(function(){
q.resolve(5);
},2000);
return q.promise;
}
}
});
};
我不确定为什么会发生这种情况,因为打开外部弹出窗口不依赖于打开内部弹出窗口。
见 plunkr :这里
更新
有趣的是,即使您在尝试使用解析参数打开另一个模态之前关闭了一个模态 - 它不会显示,直到在第二个模态上完成解析,即使与第一个无关 - 这不是我知道的真实世界场景,但我'我很想知道为什么。
$scope.doesntWork = function(){
$timeout(function(){
$uibModal.open({
templateUrl : 'popup_inner.html'
});
},1);
//unrelated to the above modal yet blocks it from showing until
//resolved finished
$uibModal.open({
templateUrl : 'popup.html',
resolve : {
test : function(){
var q = $q.defer();
setTimeout(function(){
q.resolve(5);
},2000);
return q.promise;
}
}
});
};