使用 bootstrap 关闭 angular.js 中的所有对话框
Close all dialog boxes in angular.js with bootstrap
我有一个生成对话框的示例,通过单击 "show modal" 按钮,此处加载对话框,然后如果我单击此模式的按钮 "open other modal",第二个对话框打开。当我单击任何模式的取消按钮时,我需要它,关闭对话框。当前打开第二个对话框,如果我单击取消,只有第二个关闭,当我尝试在第一个对话框中单击取消时,它不会关闭。我能做什么?
var modalInstance = null;
var modalScope = $scope.$new();
$scope.close = function (ok, hide) {
if(ok) {
//alert('ok');
} else {
//alert('cancel');
}
modalInstance.dismiss();
};
$scope.showModal = function() {
modalInstance = $modal.open({
templateUrl: 'myModal.html',
scope: modalScope
});
}
$scope.otherModal = function() {
modalInstance = $modal.open({
templateUrl: 'myModal2.html',
scope: modalScope
});
}
您需要跟踪正在创建的模态框。这是一个简单的例子,模态被保存在一个数组中。可能有更好的解决方案,但这为您提供了解决问题的提示。
var modalInstances = [];
var modalScope = $scope.$new();
$scope.close = function (ok, hide) {
if(ok) {
//alert('ok');
} else {
//alert('cancel');
}
if(modalInstances.length > 0){
modalInstances[modalInstances.length-1].dismiss();
modalInstances.pop();
}
};
$scope.showModal = function() {
modalInstances.push($modal.open({
templateUrl: 'myModal.html',
scope: modalScope
}));
}
$scope.otherModal = function() {
modalInstances.push($modal.open({
templateUrl: 'myModal2.html',
scope: modalScope
}));
}
您可以给它们一个不同的变量名,然后将它们传递给模板中的关闭函数。然后有条件地解雇他们,请看下文。
angular.module("myApp", ['ui.bootstrap'])
.controller("MyCtrl", function($scope, $modal) {
var modalInstanceOne,
modalInstanceTwo;
var modalScope = $scope.$new();
$scope.close = function(modal) {
return (modal === 'modalInstanceOne') ? modalInstanceOne.dismiss() : modalInstanceTwo.dismiss();
};
$scope.showModal = function() {
modalInstanceOne = $modal.open({
templateUrl: 'myModal.html',
scope: modalScope
});
}
$scope.otherModal = function() {
modalInstanceTwo = $modal.open({
templateUrl: 'myModal2.html',
scope: modalScope
});
}
});
<div ng-app="myApp">
<!-- FIRST MODAL -->
<script type="text/ng-template" id="myModal.html">
<div class="modal-header">
<h3 class="modal-title">Modal title</h3>
</div>
<div class="modal-body">
<button type='button' ng-click='otherModal()'>open other modal</button>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="close(true)">OK</button>
<button class="btn btn-warning" ng-click="close('modalInstanceOne')">Cancel</button>
</div>
</script>
<!-- SECOND MODAL -->
<script type="text/ng-template" id="myModal2.html">
<div class="modal-header">
<h3 class="modal-title">OTHER MODAL</h3>
</div>
<div class="modal-body">
Modal content
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="close(true)">OK</button>
<button class="btn btn-warning" ng-click="close('modalInstanceTwo')">Cancel</button>
</div>
</script>
<div ng-controller="MyCtrl">
<input type="button" value="Show modal" ng-click="showModal()" />
</div>
我有一个生成对话框的示例,通过单击 "show modal" 按钮,此处加载对话框,然后如果我单击此模式的按钮 "open other modal",第二个对话框打开。当我单击任何模式的取消按钮时,我需要它,关闭对话框。当前打开第二个对话框,如果我单击取消,只有第二个关闭,当我尝试在第一个对话框中单击取消时,它不会关闭。我能做什么?
var modalInstance = null;
var modalScope = $scope.$new();
$scope.close = function (ok, hide) {
if(ok) {
//alert('ok');
} else {
//alert('cancel');
}
modalInstance.dismiss();
};
$scope.showModal = function() {
modalInstance = $modal.open({
templateUrl: 'myModal.html',
scope: modalScope
});
}
$scope.otherModal = function() {
modalInstance = $modal.open({
templateUrl: 'myModal2.html',
scope: modalScope
});
}
您需要跟踪正在创建的模态框。这是一个简单的例子,模态被保存在一个数组中。可能有更好的解决方案,但这为您提供了解决问题的提示。
var modalInstances = [];
var modalScope = $scope.$new();
$scope.close = function (ok, hide) {
if(ok) {
//alert('ok');
} else {
//alert('cancel');
}
if(modalInstances.length > 0){
modalInstances[modalInstances.length-1].dismiss();
modalInstances.pop();
}
};
$scope.showModal = function() {
modalInstances.push($modal.open({
templateUrl: 'myModal.html',
scope: modalScope
}));
}
$scope.otherModal = function() {
modalInstances.push($modal.open({
templateUrl: 'myModal2.html',
scope: modalScope
}));
}
您可以给它们一个不同的变量名,然后将它们传递给模板中的关闭函数。然后有条件地解雇他们,请看下文。
angular.module("myApp", ['ui.bootstrap'])
.controller("MyCtrl", function($scope, $modal) {
var modalInstanceOne,
modalInstanceTwo;
var modalScope = $scope.$new();
$scope.close = function(modal) {
return (modal === 'modalInstanceOne') ? modalInstanceOne.dismiss() : modalInstanceTwo.dismiss();
};
$scope.showModal = function() {
modalInstanceOne = $modal.open({
templateUrl: 'myModal.html',
scope: modalScope
});
}
$scope.otherModal = function() {
modalInstanceTwo = $modal.open({
templateUrl: 'myModal2.html',
scope: modalScope
});
}
});
<div ng-app="myApp">
<!-- FIRST MODAL -->
<script type="text/ng-template" id="myModal.html">
<div class="modal-header">
<h3 class="modal-title">Modal title</h3>
</div>
<div class="modal-body">
<button type='button' ng-click='otherModal()'>open other modal</button>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="close(true)">OK</button>
<button class="btn btn-warning" ng-click="close('modalInstanceOne')">Cancel</button>
</div>
</script>
<!-- SECOND MODAL -->
<script type="text/ng-template" id="myModal2.html">
<div class="modal-header">
<h3 class="modal-title">OTHER MODAL</h3>
</div>
<div class="modal-body">
Modal content
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="close(true)">OK</button>
<button class="btn btn-warning" ng-click="close('modalInstanceTwo')">Cancel</button>
</div>
</script>
<div ng-controller="MyCtrl">
<input type="button" value="Show modal" ng-click="showModal()" />
</div>