Skip to content

Instantly share code, notes, and snippets.

@lutsen
Last active August 29, 2015 14:14
Show Gist options
  • Save lutsen/c764fa9207783d0153e2 to your computer and use it in GitHub Desktop.
Save lutsen/c764fa9207783d0153e2 to your computer and use it in GitHub Desktop.
An Angular directive to show a javascipt confirmation message when the user clicks a page element. The ng-confirm-click attribute contains the text in the confirmation window. The confirmed-click attribute contains the actual ation that is executed if the user confirms.
// http://stackoverflow.com/questions/18313576/confirmation-dialog-on-ng-click-angularjs
// http://plnkr.co/edit/YWr6o2?p=preview
angular.module('yourApp')
.directive('ngConfirmClick', function () {
return {
link: function (scope, element, attr) {
var msg = attr.ngConfirmClick || "Are you sure?";
var clickAction = attr.confirmedClick;
element.bind('click',function (event) {
if ( window.confirm(msg) ) {
scope.$apply(clickAction)
}
});
}
};
});
<button confirmed-click="delete($index, '{{object.id}}')" ng-confirm-click="Are you sure you want to delete {{object.name}}?">
Delete
</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment