Personally, I would approach the problem a different way all together.
Trying to intercept the click and stop propagation sees hack-ish. No?
Other approach ideas:
- put a flag on rootScope defining whether the user is logged in or not and
reference it in the logic functions like ng-click or ng-disabled
- use a service in your controller that checks user logged in state and
call this function in any method on your scope (same as above but service
function instead of scope flag)
- create your own directive in place of ng-click:
<div user-click="myAction()">Do it !</div>
app.directive('userClick', function() {
return {
priority: -100
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function(e) {
if(!isRegistered()) {
//do something for logged out user
}
else{
//call function on parent scope
scope.myAction();
}
});
}
};
});
On Wednesday, April 16, 2014 2:22:36 AM UTC-6, Michel Parpaillon wrote:
>
> Hi everyone,
> I already post this issue on Stack Overflow but nobody seems to be able to
> help me. So here I am.
>
> Some actions in my Angular app require the user to be registered. If the
> user is not, we want to show him a "Register modal" and prevent the
> original action.
> Those actions can be triggered via ng-click or any other "click binding"
> directive (for example the 'modal-toggle' one).
>
> So I found this solution: http://stackoverflow.com/a/16211108/2719044
> This is pretty cool but only works with ng-click.
> I first wanted to make the "terminal" property of the directive dynamic
> but couldn't manage to do it. So the idea was to set "terminal" to true and
> manually prevent default click action in the directive.
> Here is my DOM:
>
> <!-- This can work with terminal:true and scope.$eval(attrs.ngClick) (see
> example above) -->
> <div user-needed ng-click="myAction()">Do it !</div>
>
> <!-- This doesn't work. I can't manage to prevent the modal-toggle to be
> executed -->
> <div user-needed modal-toggle="my-modal-id-yey">Show yourself modal !</div>
>
> And my directive(s) (which don't work...)
>
> // First try (with terminal:true)
> app.directive('userNeeded', function() {
> return {
> priority: -100,
> terminal: true,
> restrict: 'A',
> link: function(scope, element, attrs) {
> element.bind('click', function(e) {
> if(isRegistered()) {
> // Here we do the action like scope.$eval or something
> }
> });
> }
> };
> });
>
> // Second try (with stopPropagation)
> app.directive('userNeeded', function() {
> return {
> priority: -100
> restrict: 'A',
> link: function(scope, element, attrs) {
> element.bind('click', function(e) {
> if(!isRegistered()) {
> e.stopPropagation();
> }
> });
> }
> };
> });
>
> ...And that's why I'm here.
> Any idea ? Thanks a lot.
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.