Skip to content

Instantly share code, notes, and snippets.

@EpokK
Created July 18, 2014 07:07
Show Gist options
  • Save EpokK/a7e794c73f1747a46a6a to your computer and use it in GitHub Desktop.
Save EpokK/a7e794c73f1747a46a6a to your computer and use it in GitHub Desktop.
ng-really? An AngularJS directive that creates a confirmation dialog for an action.
/**
* A generic confirmation for risky actions.
* Usage: Add attributes: ng-really-message="Really?" ng-really-click="takeAction()" function
*/
angular.module('app').directive('ngReallyClick', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
var message = attrs.ngReallyMessage;
if (message && confirm(message)) {
scope.$apply(attrs.ngReallyClick);
}
});
}
}
}]);
@EpokK
Copy link
Author

EpokK commented Nov 3, 2014

app.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.$eval(clickAction)
                        }
                    });
                }
            };
    }]);

@tanftw
Copy link

tanftw commented Jun 5, 2015

I love this 😃 . Really simple and useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment