Skip to content

Instantly share code, notes, and snippets.

@asafge
Created November 12, 2013 13:06
Show Gist options
  • Select an option

  • Save asafge/7430497 to your computer and use it in GitHub Desktop.

Select an option

Save asafge/7430497 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="Are you sure"? 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);
}
});
}
}
}]);
@dhlavaty

Copy link
Copy Markdown

@asafge -> Yes, this is a possible solution. But question is: Is it a memory leak, or not ?

@emw-ghertner

Copy link
Copy Markdown

Thank you!

@sampov2

sampov2 commented May 2, 2014

Copy link
Copy Markdown

@dhlavaty it's more like a logic leak. If something removes this attribute (which is rare though), this handler will still be there, and clicking the element will trigger the functionality. If this element is removed from the DOM, the click handler is cleaned up and as such, it would be cleaned up properly. I haven't seen directives or other angular code which dynamically fiddles with the existence of attributes. Taking this into consideration, this directive can be considered safe.

@dhlavaty

dhlavaty commented May 5, 2014

Copy link
Copy Markdown

In my case, I use ng-really for a delete action. I have a table with records, and each record has (in last column) a DELETE button with ng-really. When user confirms deletion, particular record is deleted from collection and Angular will "repaint" the table (a.k.a. removes the whole table row with record). And now I'm not really sure, if Angular will safely remove 'click' handler itself or not.

PS: Of course, using '$destroy' event as described above is a good practice, but I'm still wondering if I understand it correctly.

@gamikun

gamikun commented Jul 8, 2014

Copy link
Copy Markdown

Thanks :D

@neilellis

Copy link
Copy Markdown

Awesome thank you!

@spinybeast

Copy link
Copy Markdown

so easy & so awesome! thank you so much!

@javiros

javiros commented Oct 24, 2014

Copy link
Copy Markdown

+1 Thank you!

@arkka

arkka commented Dec 3, 2014

Copy link
Copy Markdown

Thanks. Very straight forward and useful.

@itmammoth

Copy link
Copy Markdown

it's really helpful!

@phonyphonecall

Copy link
Copy Markdown

Clean and simple. Thanks.

@breadcrumble

Copy link
Copy Markdown

This is great and easy to use. Thanks!

@mikethejet

Copy link
Copy Markdown

Looks good but, i dont know why but my confirm action is called twice (so http ajax) loading the page for two times... is there any reason ?
(script included only once)
áááááh the directive was included for twice time ..sorry ! ;)

@OKNoah

OKNoah commented Mar 25, 2015

Copy link
Copy Markdown

Is this available as a module through Bower?

@marianoqueirel

Copy link
Copy Markdown

hello, thank it served me.
a query, if confirmed execute an action , but if not confirmed , I just submit the form , I need to do nothing , to if
if ( message && confirm ( message) ) {
$ scope apply ( attrs.ngReallyClick ) . ;
} else {
$ scope apply ( void (0 ) ) . ;
}

I put something, so you do not run nothing but works for me , just send the form where I have incorporated ng - really- click

@vinibol12

Copy link
Copy Markdown

great job! worked perfectly!

@renatojf

Copy link
Copy Markdown

great snippet!

@maciej-gurban

Copy link
Copy Markdown

Thanks!

@fbriou

fbriou commented Dec 8, 2015

Copy link
Copy Markdown

Top! Thank you.

@nhobi

nhobi commented Apr 6, 2016

Copy link
Copy Markdown

This is so great. Thanks!

@joelgarciajr84

Copy link
Copy Markdown

Awesome man! 👍

@douglasFranco

Copy link
Copy Markdown

Beginner saved. Tnahk you!

@rkingon

rkingon commented Aug 19, 2016

Copy link
Copy Markdown

FYI - you don't need to clean this up afterwards w/ $destroy because the element that has the event listener is already being removed which also kills the bind (personally though, I would use on over bind

@yvaldez087

Copy link
Copy Markdown

Hi,
Im using a function just like this but i have an issue as other guys told, everytime l select cancel button the requested post it increments one at time, This way when l finally confirm the modal lt sends all the cumulative requests.
Someone have this issue?

ghost commented Oct 25, 2016

Copy link
Copy Markdown

thx

@f2001340

f2001340 commented Jan 4, 2017

Copy link
Copy Markdown

Thankyou! Good Stuff.

@pragyeshthakur

Copy link
Copy Markdown

Thank you somuch...

@prashantbiradar92

Copy link
Copy Markdown

nice na !!!

@muthubonzi

Copy link
Copy Markdown

confirm action is called thrice :( and ajax post is called thrice in my case, can someone help?

@dimaanj

dimaanj commented Sep 9, 2019

Copy link
Copy Markdown

Thanks

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