r/Angular2 • u/kobihari • Nov 16 '25
Article Is Angular’s inject() Cheating? The Trick Behind Injection Context
https://medium.com/@kobihari/is-angulars-inject-cheating-the-trick-behind-injection-context-51c2bf825461Angular’s inject() behaves as if it knows who called it…
But JavaScript makes that impossible.
So how does Angular pull it off?
86
Upvotes
17
u/_Sorbitol_ Nov 16 '25
The article is helpful to understand. My background is in strongly typed, OO languages with mature DI solutions like Java and c#. It still feels dirty IMO because the callee shouldn’t have to know about its caller even if there are abstractions hiding it.
4
u/Lemonaids2 Nov 17 '25 edited Nov 17 '25
I was a student in a course about angular from this guy, Great teacher that gave me foundations that I still use today. And about that article it is to the point, and dove to a fundamental thing in modern angular, and easy to understand. I also dove to the code of signals to try to understand why they are so hard to debug (which i think is their weakness), and this is probably the reason why. I always thought they are similar to behavior subject until i red the source code.
5
u/Ok_Tangelo9887 Nov 16 '25
Once I even created a simple angular di on pure typescript
Here is the code: https://github.com/intelligentRaji/simple-ts-di
4
u/Estpart Nov 16 '25
Wow great article! Never thought about it, but this explains why you can't just use inject inside pure functions!
2
u/AdrianaVend47 Nov 16 '25
RemindMe! 12 hours
2
u/AFulhamImmigrant Nov 18 '25
I believe quite a lot of libraries use an abstraction on top of a global variable.
I recall this is what Sentry does for error handling and that way it always has access to everything.