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
{{ message }}
Repository navigation
Feature: Add/Attach directive to dynamically created component (to component host) #47728
Which @angular/* package(s) are relevant/related to the feature request?
core
Description
Currently when I am creating component dynamically, using ViewContainerRef.createComponent() (or same works for createComponent function) i have no option how to add/attach directive to newly created component.
I case that i have list of dynamic components (various components) rendered, all implements same interface it would be awesome to have option to dynamically add/attach Directive to host element of component.
This would add ability to creator of dynamic component to manage which directives are attached to which dynamic component instance.
Proposed solution
Extend options for ViewContainerRef.createComponent() (also for createComponent function) with array of "Type" which would create also instances of specified directives on component host with all life cycle stuff.
This will be possible with the new directive composition API in Angular v15. E.g. on the component that you're passing into ViewContainerRef.createComponent, you can do the following:
@Component({
hostDirectives: [ExtraDirOne, ExtraDirTwo]
})
class SomeComp {}
@crisbeto I think the feature here was passing each time a different directive. Host directives are set at compile time. You can't pass them dynamically to a dynamic component.
Anyway will it be possible to use setInput for host directive input on dynamic component?
Regarding changing the directives dynamically: it's unlikely that we'll support it since we generally don't want to have multiple APIs that roughly the same thing. Also if we allow both hostDirectives and directives passed through createComponent, the execution order is unclear.
Regarding setInput: it works on host directives as well.
@crisbeto@pkozlowski-opensource well this is not good, because we have dynamic components, that are created not by us, but only consumed by us and i need to attach directive to these "3rd party components". And also attaching these directives is configurable, so that means once again we will have to to it old way, that means manualy creating directive instance and calling all its lifecycle methods manually to work properly.
These directives are not meant to have inputs and outputs just modify and add some functionality over html element.
So once again there will be no clean solution, only work arounds and "hacking". :(
PS: thanks to host directives it will be easier to do "hacking" but still its hacking.
Regarding changing the directives dynamically: it's unlikely that we'll support it since we generally don't want to have multiple APIs that roughly the same thing. Also if we allow both hostDirectives and directives passed through createComponent, the execution order is unclear.
Regarding setInput: it works on host directives as well.
Concerning order - I think that's pretty much the same as regular directives. Imagine a component with host directive which I declare in the html. I can put a custom directive on it and there is a known order of execution. I see no difference from creating a dynamic component and adding a directive to it. It's the same as any dynamic API compared to static API in template.
@crisbeto But it's not the same, doesn't it? Without the possibility to pass directives through createComponent, it would mean the creation of wrapped components all the time.
we generally don't want to have multiple APIs that roughly the same thing.
@pkozlowski-opensource@crisbeto guys is this even worth consideration? This feature would make awesome change to using angular dynamically. As @Harpush said this should not be problem.
Thx
angular-automatic-lock-bot commented on Dec 8, 2022
Activity
crisbeto commentedon Oct 26, 2022
This will be possible with the new directive composition API in Angular v15. E.g. on the component that you're passing into
ViewContainerRef.createComponent, you can do the following:Harpush commentedon Oct 26, 2022
@crisbeto I think the feature here was passing each time a different directive. Host directives are set at compile time. You can't pass them dynamically to a dynamic component.
Anyway will it be possible to use setInput for host directive input on dynamic component?
mlc-mlapis commentedon Oct 26, 2022
@crisbeto It's not certainly sure how exactly you think your snippet would behave, but it looks like a static definition.
crisbeto commentedon Oct 26, 2022
Regarding changing the directives dynamically: it's unlikely that we'll support it since we generally don't want to have multiple APIs that roughly the same thing. Also if we allow both
hostDirectivesand directives passed throughcreateComponent, the execution order is unclear.Regarding
setInput: it works on host directives as well.mlc-mlapis commentedon Oct 26, 2022
Fine, now it's clear, thanks.
I hope, there will be a way how to define the default behavior.
kukjevov commentedon Oct 27, 2022
@crisbeto @pkozlowski-opensource well this is not good, because we have dynamic components, that are created not by us, but only consumed by us and i need to attach directive to these "3rd party components". And also attaching these directives is configurable, so that means once again we will have to to it old way, that means manualy creating directive instance and calling all its lifecycle methods manually to work properly.
These directives are not meant to have inputs and outputs just modify and add some functionality over html element.
So once again there will be no clean solution, only work arounds and "hacking". :(
PS: thanks to host directives it will be easier to do "hacking" but still its hacking.
Harpush commentedon Oct 27, 2022
Concerning order - I think that's pretty much the same as regular directives. Imagine a component with host directive which I declare in the html. I can put a custom directive on it and there is a known order of execution. I see no difference from creating a dynamic component and adding a directive to it. It's the same as any dynamic API compared to static API in template.
mlc-mlapis commentedon Oct 27, 2022
@crisbeto But it's not the same, doesn't it? Without the possibility to pass directives through
createComponent, it would mean the creation of wrapped components all the time.kukjevov commentedon Nov 7, 2022
@pkozlowski-opensource @crisbeto guys is this even worth consideration? This feature would make awesome change to using angular dynamically. As @Harpush said this should not be problem.
Thx
angular-automatic-lock-bot commentedon Dec 8, 2022
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.