Skip to content

feat(compiler): support .prevent and .stop modifier for event bubbling#67865

Open
splincode wants to merge 2 commits intoangular:mainfrom
splincode:splincode/feat-prevent
Open

feat(compiler): support .prevent and .stop modifier for event bubbling#67865
splincode wants to merge 2 commits intoangular:mainfrom
splincode:splincode/feat-prevent

Conversation

@splincode
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #4059

Motivation

Angular currently allows an ambiguous case for event binding on a component host: if an event name matches an @Output, but the same DOM event also bubbles from inside the component template, the parent handler may be attached as a fallback DOM listener even when the expected behavior is to subscribe only to the component output.

This makes the behavior non-obvious and harder to debug.

<custom-change (change)="onChange()"></custom-change>
@Component({
  selector: 'custom-change',
  template: '<input>', // bubbling native `change`
})
export class CustomChange {}

In this case, (change) on the component host may work through the fallback DOM mechanism because the native change event bubbles, even though the component does not declare an explicit change output.

The behavior feels "magical" and does not clearly signal an API issue in the component.

What is the new behavior?

<custom-change (change)="onChange()"></custom-change>
@Component({
  selector: 'custom-change',
  template: '<input (change.prevent)="...">'
})
export class CustomChange {}

Does this PR introduce a breaking change?

  • Yes
  • No

@pullapprove pullapprove bot requested a review from AndrewKushnir March 25, 2026 17:32
@leonsenft leonsenft added the area: core Issues related to the framework runtime label Mar 25, 2026
@ngbot ngbot bot added this to the Backlog milestone Mar 25, 2026
@thePunderWoman thePunderWoman added the needs: discussion Indicates than an issue is an open discussion label Mar 25, 2026
Comment on lines +122 to +156
function parseEventName(eventName: string): {eventName: string; prevent: boolean; stop: boolean} {
const [name, ...modifiers] = eventName.split('.');
if (modifiers.length === 0) {
return {eventName, prevent: false, stop: false};
}

let prevent = false;
let stop = false;
for (const modifier of modifiers) {
if (modifier === 'prevent') {
prevent = true;
} else if (modifier === 'stop') {
stop = true;
} else {
return {eventName, prevent: false, stop: false};
}
}

return {eventName: name, prevent, stop};
}

function applyEventModifiers(
handler: Function,
{prevent, stop}: {prevent: boolean; stop: boolean},
): Function {
return (event: Event) => {
if (prevent) {
event.preventDefault();
}
if (stop) {
event.stopPropagation();
}
return handler(event);
};
}
Copy link
Member

@JeanMeche JeanMeche Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issues I see with this approach:

  • non tree shakable
  • not discoverable via ALS
  • no compile checks.

I'm wondering if we should back this into the compiler directly as explored in #67076.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's true, your idea is really great

@angular-robot angular-robot bot added the detected: feature PR contains a feature commit label Mar 27, 2026
@splincode
Copy link
Contributor Author

@JeanMeche I updated, is it right way?

@splincode splincode changed the title feat(platform-browser): support .prevent and .stop modifier for event bubbling feat(compiler): support .prevent and .stop modifier for event bubbling Mar 27, 2026
@splincode splincode requested a review from JeanMeche March 27, 2026 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Issues related to the framework runtime detected: feature PR contains a feature commit needs: discussion Indicates than an issue is an open discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants