-
Notifications
You must be signed in to change notification settings - Fork 27.2k
feat(core): auto-resolve duplicate hostDirectives matches to avoid NG0309 (#57846)
#64132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason why we haven't done this ourselves, but rather we throw the error, is that directives can influence the values during DI, as well as the order in which host bindings are applied. There isn't really a good way to know if we should pick the first match or one of the subsequent ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you provide an example of code that will not work with that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you had something like this, assuming that
dirFoois a directive that is applied twice as a host directive:The host bindings on the
divwill execute based on the matching order of the directives. This means that depending on if you decide to take the first instance ofdirFooor the second one, the result can be different. E.g. if bothdirFooanddirBarhad a host binding of[class.active], taking the first instance ofdirFoowill allowdirBarto override it, whereas if you take the second instance,dirFoowill overridedirBar.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<div dirFoo dirFoo></div>still produces NG0309hostDirectives: [DirFoo, DirFoo]as well(look at the tests)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was using the HTML to illustrate the execution order. This situation can happen if the host directives are applied through a selector-matched directive.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whenever you actually use Directive Composition API for Composition -- you run into this all the time. I have a case in the issue linked to this PR, other examples:
Iconsdirective for displaying icons that is used across all other directives, such as buttons, chips, badges, textfields. Some of those directives can be combined on one element to be sum of both things, but I cannot do that because I get two instances of icon directive.Appearancedirective for displaying interactive state. It is used everywhere as well. Again that is preventing composition of two directives that on their own both have this already applied.These are just global encounters off the top of my head.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding having to alias everything.
[(dropdownOpen)]. This directive is used, for example, in textfields to open/close datalist for selects/date-pickers/autocomplete etc. Now imagine users also import this directive on their own because the need it on some other element like a dropdown menu button in the same view. Without aliasing they now get double match since the textfield already has it as a host directive.I'm making these as concrete as I can to illustrate the issue, but really this is just a huge problem that hinders Directive Composition API very very much Basically every time you move some reusable logic into directive A and have directive B that needs it - you need to worry that there is no directive C that also needs it and that can be applied with B at the same time. And this happens a lot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@crisbeto can we prioritize this please? We need any solution as soon as possible. Currently we forced to create many
class Directive1 extends Directiveto avoid this error.I'm not pushing exactly this solution, but we need any.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We did decide to address this some time ago, but I haven't had time to sit down and figure out how we should handle it because of other priorities. I'll see if I can carve out some time...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!