Skip to content

Instantly share code, notes, and snippets.

@kwmiebach
Forked from brunovianarezende/ng-really.js
Last active February 11, 2018 13:55
Show Gist options
  • Save kwmiebach/16bdaa04611e1a3bbf478be07cde607f to your computer and use it in GitHub Desktop.
Save kwmiebach/16bdaa04611e1a3bbf478be07cde607f to your computer and use it in GitHub Desktop.
ng-confirm - An AngularJS directive that creates a confirmation dialog for an action. Forked from https://gist.github.com/asafge/7430497 and https://gist.github.com/brunovianarezende/8437155
angular.module('app')
.directive('ngReallyClick', [ function() {
/**
* A generic confirmation for risky actions.
* https://gist.github.com/kwmiebach/16bdaa04611e1a3bbf478be07cde607f
* Originally https://gist.github.com/asafge/7430497
* replaces: confirm - ng-confirm
* Usage: Add attributes:
* * ng-really-click="takeAction()" function
* * ng-really-message="Are you sure?"
* There is also another fork with bootstrap added:
* https://gist.github.com/mwagena/dd30c7803a65f6ae23ec
*/
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
var message = attrs.ngReallyMessage;
if (message && confirm(message)) {
scope.$apply(attrs.ngReallyClick);
}
});
}
};
}])
.directive('ngReallyClickIf', [function() {
/**
* Alternative version thanks to https://gist.github.com/brunovianarezende/8437155
* Adds a condition. Confirmation dialog is suppressed
* and action taken anyway if condition does not evaluate true.
* Usage: Add attributes:
* * ng-really-click-if="takeAction()" function
* * ng-really-message="Are you sure?"
* * ng-really-cond="mustBeEvaluatedToTrueForTheConfirmBoxBeShown" expression
*/
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
var condition = scope.$eval(attrs.ngReallyCond);
if(condition){
var message = attrs.ngReallyMessage;
if (message && confirm(message)) {
scope.$apply(attrs.ngReallyClickIf);
}
}
else{
scope.$apply(attrs.ngReallyClickIf);
}
});
}
};
}])
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment