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
[ ] 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
Defining a class SingletonLocator
export class SingletonLocator {
public static injector: Injector = undefined;
}
Reacted by UrosBgd, Den Klimovsky, Ingo Bürk, Abdelmoez GUETAT, Максим Иванов, Alexander Turtsevich, Samuel Fernández, Abdullah Saleem, Chellappan, Sheik Althaf and 1 more
I have Logger service which can be injected in any service/component on demand
I want to introduce some abstract class AbstractFormComponent with some common logic for all the forms in app
I want to inject Logger service into AbstractFormComponent
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:
Inject Logger in all derived classes' constructors (at least if you has to inject some other service)
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!
@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
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.
Closing as available in v14 with the inject function, which can be used in the constructor/field initialisers of injectables/components/directives/pipes.
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?
Same question here @angelaki , actually ChangeDetectionRef was not usable from the current Injector, what about inject() function? See: #14656 (comment)
Activity
ajitsinghkaler commentedon Dec 16, 2019
Can you please provide a usecase. If you can give a good use case only then it will be implemented.
xumepadismal commentedon Dec 16, 2019
Well, here is the use case:
Loggerservice which can be injected in any service/component on demandabstract class AbstractFormComponentwith some common logic for all the forms in appLoggerservice intoAbstractFormComponentLogger(because child class likely won't need it)Just imagine that abstract class has 5 dependencies, not only
Logger...Currently you have two options:
Loggerin all derived classes' constructors (at least if you has to inject some other service)And indeed it would be very useful to implement injection via decorator!
kemsky commentedon Dec 20, 2019
There is one more option, you can create single facade service which wraps all your dependencies and pass only one service to abstract class.
xumepadismal commentedon Dec 20, 2019
@kemsky you can and furthermore you can even just inject
Injectorservice instead of creating facade but...Loggerinto one concrete class of this hierarchysplincode commentedon Dec 23, 2019
@mascoun https://www.npmjs.com/package/@angular-ru/autowired
abdelmoez-guetat commentedon Dec 23, 2019
@splincode Thank you it's working 🙏
angular-robot commentedon Jun 4, 2021
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.
angular-robot commentedon Jun 26, 2021
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.
alxhub commentedon May 4, 2022
This is on our roadmap :)
splincode commentedon May 4, 2022
@alxhub amazing!!!
yjaaidi commentedon May 17, 2022
🎉 Fixed by #45991 thanks to @alxhub
JoostK commentedon May 26, 2022
Closing as available in v14 with the
injectfunction, which can be used in the constructor/field initialisers of injectables/components/directives/pipes.splincode commentedon May 27, 2022
I wanna use it inside decorators 😩
Airblader commentedon May 27, 2022
The difference between
and
seems negligible to me. You don't save anything by using a decorator.
angelaki commentedon May 30, 2022
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 usingreadonly cdr = inject(ChangeDetectorRef);in almost every component have a bad performance impact?meriturva commentedon Jun 7, 2022
Same question here @angelaki , actually
ChangeDetectionRefwas not usable from the currentInjector, what aboutinject()function? See: #14656 (comment)angelaki commentedon Jun 30, 2022
@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
angular-automatic-lock-bot commentedon Jul 31, 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.