fix(core): auto-resolve duplicate host directive matches instead of throwing NG0309#67760
Closed
splincode wants to merge 1 commit intoangular:mainfrom
Closed
fix(core): auto-resolve duplicate host directive matches instead of throwing NG0309#67760splincode wants to merge 1 commit intoangular:mainfrom
splincode wants to merge 1 commit intoangular:mainfrom
Conversation
…hrowing NG0309 When a directive is both selector-matched on an element and referenced as a hostDirective of another matched directive (or appears multiple times across nested host directive chains), silently deduplicate it rather than throwing NG0309. The first occurrence is kept; subsequent ones are ignored — mirroring how providers handle duplicates. Fixes angular#57846
Member
|
There is already #64132, no need to open duplicate PRs for something that needs some design decisons on our side. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
When a directive appeared on an element more than once — either because it was both selector-matched and pulled in as a hostDirective of another matched directive, or because it was referenced multiple times across nested host directive chains — Angular threw a hard runtime error:
NG0309: Directive Foo matches multiple times on the same element. Directives can only match an element once.
Fixes #57846
What is the new behavior?
Angular silently deduplicates instead of throwing. The first occurrence wins and all subsequent occurrences of the same directive on the same element are ignored. The directive is instantiated exactly once, regardless of how many times it appears across selector matches and host directive chains.
This mirrors how providers behave when the same token appears in multiple providers arrays — no error, first registration is kept.
Execution order is preserved: since host directives are always inserted before their host in the directive list, a directive that enters via a hostDirective chain will still execute before the selector-matched directive that hosts it, even if it was also independently selector-matched.
Does this PR introduce a breaking change?