Skip to content

Feature request : Allow properties Injection #34425

Closed

Description

@abdelmoez-guetat

🚀 feature request

[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:

Description

Give the possibility to inject somthing without using constructor

Current behavior

All the dependencies must be injected in constructor:

Expected behavior

Add new annotation @Autowire for the property which can be present with @Optional, for example:

export class MyClass {
  @Autowire
  private mainClass: MainClass;
}

Current solution

  1. Defining a class SingletonLocator
export class SingletonLocator {
  public static injector: Injector = undefined;
}
  1. Add in AppModule
export class AppModule {
    constructor(injector: Injector) {
        SingletonLocator.injector = injector;
    }
}
  1. Retrive Singleton in a Component
@Component(..)
export class MyComponent {
 
    private myService: MyService;

   constructor() {
     this.myService = SingletonLocator.get(MyService);
  }
}

Activity

  1. ajitsinghkaler commented on Dec 16, 2019

    @ajitsinghkaler
    Contributor

    Can you please provide a usecase. If you can give a good use case only then it will be implemented.

  2. added
    area: coreIssues related to the framework runtime
    on Dec 16, 2019
  3. added this to the needsTriage milestone on Dec 16, 2019
  4. xumepadismal commented on Dec 16, 2019

    @xumepadismal

    Well, here is the use case:

    1. I have Logger service which can be injected in any service/component on demand
    2. I want to introduce some abstract class AbstractFormComponent with some common logic for all the forms in app
    3. I want to inject Logger service into AbstractFormComponent
    4. Meantime I want derived classes to be able to inject services but don't want to force them to inject also Logger (because child class likely won't need it)

    Just imagine that abstract class has 5 dependencies, not only Logger...

    Currently you have two options:

    1. Inject Logger in all derived classes' constructors (at least if you has to inject some other service)
    2. Use a hacky workaround as @mascoun described (actually we use something similar in our project currently)

    And indeed it would be very useful to implement injection via decorator!

  5. added
    featureLabel used to distinguish feature request from other issues
    on Dec 17, 2019
  6. modified the milestones: needsTriage, Backlog on Dec 17, 2019
  7. kemsky commented on Dec 20, 2019

    @kemsky

    There is one more option, you can create single facade service which wraps all your dependencies and pass only one service to abstract class.

  8. xumepadismal commented on Dec 20, 2019

    @xumepadismal

    @kemsky you can and furthermore you can even just inject Injector service instead of creating facade but...

    • ...what if abstract class is part of a third-party library?
    • ...classes can have complicated hierarchy with different dependencies
    • ...and it's not super convenient to introduce facade service and refactor all classes in heirarchy just because you need to inject Logger into one concrete class of this hierarchy
  9. splincode commented on Dec 23, 2019

    @splincode
    Contributor
  10. abdelmoez-guetat commented on Dec 23, 2019

    @abdelmoez-guetat
    Author

    @splincode Thank you it's working 🙏

  11. angular-robot commented on Jun 4, 2021

    @angular-robot
    Contributor

    Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

    Find more details about Angular's feature request process in our documentation.

  12. angular-robot commented on Jun 26, 2021

    @angular-robot
    Contributor

    Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

    We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

    You can find more details about the feature request process in our documentation.

  13. alxhub commented on May 4, 2022

    @alxhub
    Member

    This is on our roadmap :)

  14. splincode commented on May 4, 2022

    @splincode
    Contributor

    @alxhub amazing!!!

  15. yjaaidi commented on May 17, 2022

    @yjaaidi
    Contributor

    🎉 Fixed by #45991 thanks to @alxhub

  16. JoostK commented on May 26, 2022

    @JoostK
    Member

    Closing as available in v14 with the inject function, which can be used in the constructor/field initialisers of injectables/components/directives/pipes.

  17. splincode commented on May 27, 2022

    @splincode
    Contributor

    I wanna use it inside decorators 😩

  18. Airblader commented on May 27, 2022

    @Airblader
    Contributor

    The difference between

    @Inject()
    readonly foo: Foo;
    

    and

    readonly foo = inject(Foo);
    

    seems negligible to me. You don't save anything by using a decorator.

  19. angelaki commented on May 30, 2022

    @angelaki
    Contributor

    Just out of curiosity: I have a component base class that I'd like to always be able to access it's ChangeDetectorRef. So could using readonly cdr = inject(ChangeDetectorRef); in almost every component have a bad performance impact?

  20. meriturva commented on Jun 7, 2022

    @meriturva

    Same question here @angelaki , actually ChangeDetectionRef was not usable from the current Injector, what about inject() function? See: #14656 (comment)

  21. angelaki commented on Jun 30, 2022

    @angelaki
    Contributor

    @meriturva yeah, it's working now! I'm just not sure if I could really inject it in all of my components. Are there any side-effects I'm not aware of? I've just asked this question in Stackoverflow, maybe someone is interested / has an idea: https://stackoverflow.com/questions/72818023/inject-changedetectorref-in-almost-every-component

  22. angular-automatic-lock-bot commented on Jul 31, 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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtimecore: difeatureLabel used to distinguish feature request from other issues

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions