我正在angularjs
使用angular-ui
库(特别是angular-ui-bootstrap
库)。
如何延迟关闭模式?在用户点击x
或“取消”按钮后,我需要将模式保持可见状态超过半秒,但我还没有找到办法。
我想在回调函数中添加一些超时,该函数传递给模态实例的then
函数,例如:
modalInstance.result.then(function success(){
// timeout .5 second
}, function cancel() {
// timeout .5 second
});
但回调函数中的代码实际上是在模态关闭后运行的。有没有办法做到这一点?
答案 0 :(得分:4)
您可以使用
$timeout(function() {
$modalInstance.dismiss('cancel');
}, 500);
使用单独的模态控制器完成代码: -
angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope, $modal, $log) {
$scope.items = ['item1', 'item2', 'item3'];
$scope.open = function (size) {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: size,
resolve: {
items: function () {
return $scope.items;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
});
// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.
angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $modalInstance,$timeout, items) {
$scope.items = items;
$scope.selected = {
item: $scope.items[0]
};
$scope.ok = function () {
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function () {
$timeout(function() {
$modalInstance.dismiss('cancel');
}, 500);
};
});