ng-click and event propagation/bubbling in 10.7

4,831 views
Skip to first unread message

max...@googlemail.com

unread,
Mar 15, 2012, 6:19:51 AM3/15/12
to AngularJS
I follow yor great work for a while now and I am really happy to say:
Congrats for 1.0.


I have a question about ng-click:
The click-event does seem to bubble the DOM upwards but thats not what
I expect after reading

"Events that are handled via these handler are always configured not
to propagate further."

in line 12889 of http://code.angularjs.org/1.0.0rc1/angular-1.0.0rc1.js.

adding "event.stopPropagation()" in line 12900 prevents the event from
bubbling,
but changing the angular code myself implies to me a lack of insight.

Can you clarify that?

Thanks in advance,
Max

Misko Hevery

unread,
Mar 15, 2012, 1:22:46 PM3/15/12
to ang...@googlegroups.com
documentation bug. It used to not bubble, but now it does. If you want to prevent then do do something like this.

<span ng-click="myBehavior(); $event.stopPropagation()">

-- Misko


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.


max...@googlemail.com

unread,
Mar 15, 2012, 6:13:18 PM3/15/12
to AngularJS
Too bad, because I need this all the time and writing
$event.stopPropagation() feels cumbersome.
(I think I understand why you changed that though: It is easier to
disable bubbling - as you showed - than to enable it manually)

If someone doesn't like bubbling too (well actually I like it, but not
always):
http://jsfiddle.net/FZvSY/1/

I'm not too happy with that solution, any suggestions?

Cheers,
Max



On 15 Mrz., 18:22, Misko Hevery <mi...@hevery.com> wrote:
> documentation bug. It used to not bubble, but now it does. If you want to
> prevent then do do something like this.
>
> <span ng-click="myBehavior(); $event.stopPropagation()">
>
> -- Misko
>
> On Thu, Mar 15, 2012 at 3:19 AM, maxm...@googlemail.com <
>
>
>
>
>
>
>
> maxm...@googlemail.com> wrote:
> > I follow yor great work for a while now and I am really happy to say:
> > Congrats for 1.0.
>
> > I have a question about ng-click:
> > The click-event does seem to bubble the DOM upwards but thats not what
> > I expect after reading
>
> > "Events that are handled via these handler are always configured not
> > to propagate further."
>
> > in line 12889 ofhttp://code.angularjs.org/1.0.0rc1/angular-1.0.0rc1.js.

Igor Minar

unread,
Mar 15, 2012, 6:22:17 PM3/15/12
to ang...@googlegroups.com
shorter version: http://jsfiddle.net/FZvSY/2/

max...@googlemail.com

unread,
Mar 17, 2012, 4:32:34 AM3/17/12
to AngularJS
cool!

Do you know why original code (and my fiddle) uses
- $parse
- var fn
- $apply
instead of scope.$eval?

On 15 Mrz., 23:22, Igor Minar <i...@angularjs.org> wrote:
> shorter version:http://jsfiddle.net/FZvSY/2/
>
> On Thu, Mar 15, 2012 at 3:13 PM, maxm...@googlemail.com
>
>
>
>
>
>
>
> <maxm...@googlemail.com> wrote:
> > Too bad, because I need this all the time and writing
> > $event.stopPropagation() feels cumbersome.
> > (I think I understand why you changed that though: It is easier to
> > disablebubbling- as you showed - than to enable it manually)
>
> > If someone doesn't likebubblingtoo (well actually I like it, but not

Vojta Jína

unread,
Mar 19, 2012, 3:00:42 AM3/19/12
to ang...@googlegroups.com
The original code parses the expression on its own, so that it can pass "locals" - $event. So the result is, that your expression has access to $event. E.g. ng-click="some($event)"

In Igor's code - he is doing scope.$eval(expre, locals) - which is not how $eval currently works - it does not pass locals.
But I think it should, here is a quick PR to make scope.$eval passing locals: https://github.com/angular/angular.js/pull/804

I actually think that Igor's example works completely differently, than expected. The custom directives are not used, only the default angular ones...
See this: http://jsfiddle.net/vojtajina/FZvSY/3/ - the id3 will never be fired, that's why you always have access to $event, because the custom directive is actually not applied.

Here it is with the directive applied: http://jsfiddle.net/vojtajina/FZvSY/5/
And the $event is undefined now... because scope.$eval does not pass locals...
The same with angular from latest build (from the PR): http://jsfiddle.net/vojtajina/FZvSY/6/

V.

> >> > For more options, visit this group at
> >> >http://groups.google.com/group/angular?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups "AngularJS" group.
> > To post to this group, send email to ang...@googlegroups.com.
> > To unsubscribe from this group, send email to angular+unsubscribe@googlegroups.com.

max...@googlemail.com

unread,
Mar 20, 2012, 12:30:37 PM3/20/12
to AngularJS
ok, it took me a while to understand this, ....
so the custom directives don't work in any case, right...?

Is there another way to implement this?

Cheers,
Max


On 19 Mrz., 08:00, Vojta Jína <vojta.j...@gmail.com> wrote:
> The original code parses the expression on its own, so that it can pass
> "locals" - $event. So the result is, that your expression has access to
> $event. E.g. ng-click="some($event)"
>
> In Igor's code - he is doing scope.$eval(expre, locals) - which is not how
> $eval currently works - it does not pass locals.
> But I think it should, here is a quick PR to make scope.$eval passing
> locals:https://github.com/angular/angular.js/pull/804
>
> I actually think that Igor's example works completely differently, than
> expected. The custom directives are not used, only the default angular
> ones...
> See this:http://jsfiddle.net/vojtajina/FZvSY/3/- the id3 will never be
> > > >> > angular+u...@googlegroups.com.
> > > >> > For more options, visit this group at
> > > >> >http://groups.google.com/group/angular?hl=en.
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups "AngularJS" group.
> > > > To post to this group, send email to ang...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > angular+u...@googlegroups.com.

Vojta Jína

unread,
Mar 21, 2012, 4:44:00 AM3/21/12
to ang...@googlegroups.com
What do you not understand ?

The fiddle was not working, because it was pointing to ci server and the link was already invalid.

V.

> > > >> > For more options, visit this group at
> > > >> >http://groups.google.com/group/angular?hl=en.
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups "AngularJS" group.
> > > > To post to this group, send email to ang...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
Reply all
Reply to author
Forward
0 new messages