Skip to content

Feature: Add/Attach directive to dynamically created component (to component host) #47728

Closed

Description

@kukjevov

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.

Let say createComponent<C>(componentType: Type<C>, options?: { index?: number; injector?: Injector; ngModuleRef?: NgModuleRef<unknown>; environmentInjector?: EnvironmentInjector | NgModuleRef<unknown>; projectableNodes?: Node[][]; directives?: Type<unknown>[]; }): ComponentRef<C>

Alternatives considered

Or some other way how to attach directive to dynamic component dynamically.

Activity

  1. added
    area: core
    on Oct 11, 2022
  2. added this to the needsTriage milestone on Oct 11, 2022
  3. crisbeto commented on Oct 26, 2022

    @crisbeto
    Member

    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 {}
    
  4. Harpush commented on Oct 26, 2022

    @Harpush

    @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?

  5. mlc-mlapis commented on Oct 26, 2022

    @mlc-mlapis
    Contributor

    @crisbeto It's not certainly sure how exactly you think your snippet would behave, but it looks like a static definition.

  6. crisbeto commented on Oct 26, 2022

    @crisbeto
    Member

    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.

  7. mlc-mlapis commented on Oct 26, 2022

    @mlc-mlapis
    Contributor

    Fine, now it's clear, thanks.

    I hope, there will be a way how to define the default behavior.

    Also if we allow both hostDirectives and directives passed through createComponent, the execution order is unclear.

  8. kukjevov commented on Oct 27, 2022

    @kukjevov
    ContributorAuthor

    @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.

  9. Harpush commented on Oct 27, 2022

    @Harpush

    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.

  10. mlc-mlapis commented on Oct 27, 2022

    @mlc-mlapis
    Contributor

    @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.

  11. kukjevov commented on Nov 7, 2022

    @kukjevov
    ContributorAuthor

    @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

  12. angular-automatic-lock-bot commented on Dec 8, 2022

    @angular-automatic-lock-bot

    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.

  13. locked and limited conversation to collaborators on Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions