angular -ui $modalsupports promises for a window closed or rejected with modalInstance.result.then(closeFn,dismissFn).
So, if I use modal to collect information for a new element, then I can do:
modalInstance.result.then(function (item) {
new Item(item).$save();
}, function () {
});
And html snippet:
<button class="btn btn-primary" ng-click="$close(item)">Save</button>
<button class="btn btn-warning" ng-click="$dismiss()">Cancel</button>
But the new element may have a conflict, or it may be invalid on the server side or many other things. Is there any way to catch this before the modal itself is closed? I would like to catch "Yes" (or "Close"), run my fn, which does new Item(item).$save(), and then:
- If it succeeds, keep closing the modal
- If this fails, highlight the error message
, ng-click $modal, :
<button class="btn btn-primary" ng-click="save(item)">Save</button>
:
var modalInstance = $modal.open({
templateUrl: '/partials/createItem.html',
controller: function ($scope) {
$scope.save = function(item) {
new Item(item).$save().then(function(){
modalInstance.close();
});
};
}
});
?