Looking to create a custom Angular directive using angular-bootstrap that mimics the confirm() function.
Check out the visual result and desired behavior in this plunk: http://embed.plnkr.co/27fBNbHmxx144ptrCuXV/preview
Now, I want to implement a directive to trigger the modal window:
<div ng-controller="ModalDemoCtrl">
<ul>
<li ng-repeat="item in items">
{{ item }} <a ng-really-message="Are you sure ?" ng-really-click="reallyDelete(item)">Delete</a>
</li>
</ul>
</div>
I have managed to create a functional directive using the 'confirm()' function. However, when attempting to switch to the modal window instead of confirm, an error "$digest already in progress
" is encountered.
Here's the plunk for reference: http://plnkr.co/edit/JSOInyZIvMtBZFaNvQRO?p=preview
var ModalDemoCtrl = function($scope, $modal) {
$scope.items = ['item1', 'item2', 'item3'];
$scope.reallyDelete = function(item) {
$scope.items = window._.remove($scope.items, function(elem) {
return elem != item;
});
};
};
angular.module('ngReallyClickModule', ['ui.bootstrap'])
.directive('ngReallyClick', ['$modal',
function($modal) {
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.ok = function() {
$modalInstance.close();
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
};
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
var message = attrs.ngReallyMessage || "Are you sure ?";
/*
//This works
if (message && confirm(message)) {
scope.$apply(attrs.ngReallyClick);
}
//*/
//*This doesn't work
var modalHtml = '<div class="modal-body">' + message + '</div>';
modalHtml += '<div class="modal-footer"><button class="btn btn-primary" ng-click="ok()">OK</button><button class="btn btn-warning" ng-click="cancel()">Cancel</button></div>';
var modalInstance = $modal.open({
template: modalHtml,
controller: ModalInstanceCtrl
});
modalInstance.result.then(function() {
scope.$apply(attrs.ngReallyClick); //raise an error : $digest already in progress
}, function() {
//Modal dismissed
});
//*/
});
}
}
}
]);
I