Reputation: 255
Can I write some function to cancel closing from result? Bad idea save model in ModalInstanceCtrl.
app.controller('MainController', ['$scope', '$modal', function ($scope, $modal) {
$scope.edit =function (id) {
var modal = $modal.open({
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl'
});
modal.result.then(function(model) {
if (somethink_wrong) {
***CANCEL CLOSING***
}
});
};
}]);
app.controller('ModalInstanceCtrl', ['$scope', '$modalInstance', function ($scope, $modalInstance) {
$scope.ok = function () {
$modalInstance.close($scope.model);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
}]);
Upvotes: 1
Views: 1050
Reputation: 368
Why not call a service in your ModalInstanceCtrl to check if "something is wrong", before actually closing it ? Thus, you could make that when you actually close it, the modal passed to the anonymous function in the "then" would be valid data, and you're sure nothing is wrong.
app.controller('MainController', ['$scope', '$modal', function ($scope, $modal) {
$scope.edit =function (id) {
var modal = $modal.open({
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl'
});
modal.result.then(function(model) {
*There is nothing wrong because we already checked :)*
});
};
}]);
app.controller('ModalInstanceCtrl', ['$scope', '$modalInstance', yourService function ($scope, $modalInstance, yourService) {
$scope.ok = function () {
if (yourService.checkNothingWrong()) {
$modalInstance.close($scope.model);
}
else {
*inform user something is wrong*
}
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
}]);
And injecting a service is maybe not even necessary. Why don't you simply make your validation in the modal controller ?
Upvotes: 2
Reputation: 2953
Nope, you basically can't easily. close and dismiss are kinda terminal. You are too late in the modal cycle.
The "will i close or not" logic correct place in inside your Modal controller, in the "ok" and "cancel" methods. The real question would be : why can't you do your validation here ?
Upvotes: 0