angular icon indicating copy to clipboard operation
angular copied to clipboard

fix(core): only set input on specific instance in ComponentRef.setInput

Open crisbeto opened this issue 1 year ago • 3 comments

ComponentRef.setInput works by calling setInputsForProperty internally. It can be problematic, because it will set the all inputs on the specific node matching the passed-in name, including other directives that aren't associated with the ComponentRef. Currently this isn't a problem, because the only way to get a ComponentRef is as a result of ComponentFactory.create and there is no way to apply other directives through there, however I see a couple of ways that it can manifest itself in the future:

  1. Once the runtime for host directives is implemented (after #46868), users will be able to apply directives to the root component.
  2. If we were to make it possible to inject ComponentRef in the future.

These changes add some extra logic so that the input is only assigned to the component instance that is associated with the ComponentRef.

crisbeto avatar Jul 31 '22 10:07 crisbeto

You're right that the intent with host directives is for them to work as if they were applied inside a template. I think that this case is different, because you have a reference pointing to a specific component class. As a user, I would find it odd if calling setInput would modify the input of a host directive I have no access to.

Small aside regarding tests: I have it on my list to add tests for this specific use case once I start working on the runtime implementation.

crisbeto avatar Aug 01 '22 17:08 crisbeto

You're right that the intent with host directives is for them to work as if they were applied inside a template. I think that this case is different, because you have a reference pointing to a specific component class. As a user, I would find it odd if calling setInput would modify the input of a host directive I have no access to.

@crisbeto I can see it as a different way of looking at the problem. Again, the initial assumption was that it does behave as a regular input. And it does make total sense to me in case of host directives. We can certainly review this assumption. Maybe an item on the fwk sync agenda?

pkozlowski-opensource avatar Aug 01 '22 18:08 pkozlowski-opensource

Sure, let's discuss it. I've added it to the agenda.

crisbeto avatar Aug 01 '22 18:08 crisbeto

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.