-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed bug with using angular-off-click in ng-repeat #46
Conversation
…stead of compile.
Not too sure I understand what you are saying the problem that this change fixes is. See the console output of: https://jsfiddle.net/TheSharpieOne/kff4qeqx/ (Note, I just copied the non-minified dist and added a |
What do you mean exactly? |
Hrrrmmmmm. You're right – that fiddle works properly. But it definitely doesn't work properly in my production code. I was thinking it may have to do with the version of angular, but I don't believe that's it. I'm going to keep playing with it until I can replicate in a fiddle the bug as it appears in my production code. The issue is that when I use multiple off-click directives, sometimes the off click will fail to fire, because the listener was getting overwritten since Thanks! |
There! I figured out the cause. The issue occurs when one uses a compile function in a directive with ng-repeat; compile only gets called once while link will get called every time. This StackOverflow answers explains a little bit: http://stackoverflow.com/a/29989258 Here's a fiddle that demonstrates the issue: https://jsfiddle.net/gafv1xgv/ This pull request will fix that issue and place all the code in a link function which gets executed on every instance of the directive – regardless of whether it was placed in an ng-repeat (as is to be expected). |
I see. Thanks for tracking down the issue. |
Excellent job tracking this one down, @Tibfib. |
Oh, can you guys do a release with the fix? |
@Tibfib Done - sorry about that |
@Tibfib Terribly sorry - I pushed and published without running the build first. Resolved in v1.0.7 |
I found a bug where the directive would have issues when it was used multiple times. This pull request fixes that. 🚀
In directives, the compile function itself is only called once, meaning that in the current code, elmId is set to 0 at the very beginning and never changes... This causes issues when multiple offClick directives are added since the listeners will overwrite each other (since they have the same elmId of 0).
This can be fixed be placing the code that was outside of the inner compile function on the inside, like so.
I made this change, and then changed it from a compile function to a link... since there's no need for it to be a compile. Compile is typically for early DOM manipulation – which isn't necessary. I found this article to be super helpful while I was investigating this bug: http://www.jvandemo.com/the-nitty-gritty-of-compile-and-link-functions-inside-angularjs-directives/
Thanks so much for the directive! Let me know if I missed something!
Will