You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ah okay, if it's by design then it means you should create your own ping event to be sent up rather than delegate to a component's event. That makes sense since it's better decoupling but also confusing since it's not enforced with native events like ^click
@gdi2290 That seems very verbose. So every component in the chain would need to re-send event?
Like:
-parent (listen to ping)
-- child (listen to ping and resend the ping - even if not interested in)
---- grand child (emit ping event)
@SekibOmazic I updated the example so it works. It's verbose I agree but it does make each component more loosely coupled but it forces it rather than allowing the developer to take advantage of ^
@gdi2290 OK thanks. This may by so by design but now we have 2 different behaviours:
for native DOM events bubbling up works to the root element
for custom events bubbling up works to parent only and an event has to be resent if you need it in ancestor
Not very intuitive IMHO.
Reacted by Aluan Haddad, Elvynia, Patrick Hillert, Jonathan Trang, cedvdb, Reuben Nahouraii, Edward Anderson, Ariel, Orel Eraki, Simon Bernier and 1 more
Will the event bubbling/emission solution be compatible with "routed" components?
Meaning components placed in the DOM via <router-outlet></router-outlet>. As of now, there is no way to emit events from "routed" components as far as I know.
IMO, complex applications will make heavy use of the router, not only for "pages", but also for tabs and such. Ideally, these routed components would use the same component model as custom tags in terms of input and output.
I was about to open an issue and stumbled upon this old one so I will try to revive it 😊
Use Case
The app loads dynamic markdown content. This markdown is processed with a third party library like Marked where we add a special links handler that replaces links with our custom elements (using Angular Elements). These elements trigger custom events that we want to handle in the parent component (the one that loaded the markdown).
The general idea is that we can let a 3rd party (non-Angular) library control the DOM and inject our Angular Elements but we want to keep communicating with these elements without having to dispatch bubbling events manually or using an indirection like a provider or state management etc...
Solutions
Solution A
Create a new CustomEventEmitter where we can configure bubbles and cancelable:
[OPTIONAL] Should we deprecate EventEmitter usage with Angular elements? Allowing both can be confusing?
Solution B
Create a new CustomEventEmitter which is an EventEmitter of type EventEmitter<{bubbles?: boolean, cancelable?: boolean, detail?: T}> so every event can have a different bubbling strategy... but that would be a surprising behavior so I am not a big fan of this one.
Solution C
@2fdevs idea: adding a bubbles field to the event.
It's a quick fix but it's a surprising behavior which is hard to document and mixes event data with metadata (e.g. bubbles)
This issue seems to be about @Outputs of regular components (also called "custom events" back in the day 😃) and not about the CustomEvents emitted from Angular Elements (i.e. Angular components converted to Custom Elements).
Since your use-case seems to be specific to @angular/elements, I think it would be better to open a separate issue (and potentially link to this one for context).
We discussed this feature in our triage meeting and decided that it is not something we want to support with outputs.
The semantics of an output are very clear: they provide a way to attach a handler to some action occurring on a component, directive or element that appears in a template. Changing that semantic to cover nested components, directives, or elements makes it much harder to reason about the template.
If bubbling events are required then it is still possible to use the DOM event system directly.
Also another common Angular pattern would be to use a service provided on a component that wishes to receive events and for descendant components to inject the service and use it to publish events.
Activity
PatrickJS commentedon Jun 2, 2015
^should be working for the custom event<grand-child (^ping)="onPing($event)" ></grand-child>:/SekibOmazic commentedon Jun 2, 2015
@gdi2290 By design? If so, should I close the issue? And is there a workaround?
PatrickJS commentedon Jun 2, 2015
@SekibOmazic oh no sorry, this seems like a bug since it should be working
vicb commentedon Jun 2, 2015
Dup of #1254 ?
PatrickJS commentedon Jun 2, 2015
ah okay, if it's by design then it means you should create your own
pingevent to be sent up rather than delegate to a component's event. That makes sense since it's better decoupling but also confusing since it's not enforced with native events like^clickSekibOmazic commentedon Jun 2, 2015
@gdi2290 That seems very verbose. So every component in the chain would need to re-send event?
Like:
-parent (listen to ping)
-- child (listen to ping and resend the ping - even if not interested in)
---- grand child (emit ping event)
PatrickJS commentedon Jun 2, 2015
@SekibOmazic I updated the example so it works. It's verbose I agree but it does make each component more loosely coupled but it forces it rather than allowing the developer to take advantage of
^http://plnkr.co/edit/MYYuNm?p=preview
SekibOmazic commentedon Jun 2, 2015
@gdi2290 OK thanks. This may by so by design but now we have 2 different behaviours:
Not very intuitive IMHO.
2fdevs commentedon Jun 16, 2015
+1 this needs to be reviewed I think.
@SekibOmazic as a workaround we're using CustomEvent(): https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events
Maybe a parameter (bubbles=true) when emitting the event could do the trick:
ericmdantas commentedon Jul 27, 2015
It'd be definetely nice to have the
^to work with custom events as well.61 remaining items
csquillen commentedon Nov 1, 2016
Will the event bubbling/emission solution be compatible with "routed" components?
Meaning components placed in the DOM via
<router-outlet></router-outlet>. As of now, there is no way to emit events from "routed" components as far as I know.IMO, complex applications will make heavy use of the router, not only for "pages", but also for tabs and such. Ideally, these routed components would use the same component model as custom tags in terms of input and output.
zoechi commentedon Nov 1, 2016
you can dispatch DOM events and they bubble as usual, but as of now there is no way to have bubbling events from outputs
yjaaidi commentedon Oct 29, 2020
I was about to open an issue and stumbled upon this old one so I will try to revive it 😊
Use Case
The app loads dynamic markdown content. This markdown is processed with a third party library like Marked where we add a special links handler that replaces links with our custom elements (using Angular Elements). These elements trigger custom events that we want to handle in the parent component (the one that loaded the markdown).
The general idea is that we can let a 3rd party (non-Angular) library control the DOM and inject our Angular Elements but we want to keep communicating with these elements without having to dispatch bubbling events manually or using an indirection like a provider or state management etc...
Solutions
Solution A
CustomEventEmitterwhere we can configurebubblesandcancelable:We can't use
EventEmitterbecausebubblesandcancelablewill be simply ignored and confusing when not using Angular elementsChange
ComponentNgElementStrategyin order to handleCustomEventEmitterdifferently instead of merging all emitters:angular/packages/elements/src/component-factory-strategy.ts
Line 50 in eb9eebb
Change
createCustomElementin order to handlebubblesandcancelable:angular/packages/elements/src/create-custom-element.ts
Lines 225 to 228 in eb9eebb
Change
createCustomEventto allow the control ofbubblesandcancelable:angular/aio/src/testing/dom-utils.ts
Lines 4 to 16 in 8fd25d9
[OPTIONAL] Should we deprecate
EventEmitterusage with Angular elements? Allowing both can be confusing?Solution B
Create a new
CustomEventEmitterwhich is anEventEmitterof typeEventEmitter<{bubbles?: boolean, cancelable?: boolean, detail?: T}>so every event can have a different bubbling strategy... but that would be a surprising behavior so I am not a big fan of this one.Solution C
@2fdevs idea: adding a
bubblesfield to the event.It's a quick fix but it's a surprising behavior which is hard to document and mixes event data with metadata (e.g. bubbles)
cc. @jelbourn @IgorMinar @mhevery
gkalpak commentedon Oct 29, 2020
Thx for the detailed write-up, @yjaaidi!
This issue seems to be about
@Outputs of regular components (also called "custom events" back in the day 😃) and not about the CustomEvents emitted from Angular Elements (i.e. Angular components converted to Custom Elements).Since your use-case seems to be specific to
@angular/elements, I think it would be better to open a separate issue (and potentially link to this one for context).yjaaidi commentedon Oct 29, 2020
Thanks @gkalpak! In fact, I hesitated between opening a new issue or reusing this vintage one and avoid pollution 😊
I'm opening a new one right now!
petebacondarwin commentedon Sep 16, 2021
We discussed this feature in our triage meeting and decided that it is not something we want to support with outputs.
The semantics of an output are very clear: they provide a way to attach a handler to some action occurring on a component, directive or element that appears in a template. Changing that semantic to cover nested components, directives, or elements makes it much harder to reason about the template.
If bubbling events are required then it is still possible to use the DOM event system directly.
Also another common Angular pattern would be to use a service provided on a component that wishes to receive events and for descendant components to inject the service and use it to publish events.
angular-automatic-lock-bot commentedon Oct 17, 2021
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.