question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When to use @Inject()?

See original GitHub issue

I’m submitting a…


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

When creating services, I simply inject them to a controller as follows:

constructor(private myService: MyService) {}

While reading the section of custom providers it mentions that I should use @Inject() when I want to inject a provider.

I am confused. When to use @Inject() and when it is not needed? Thanks

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
adrien2pcommented, Mar 22, 2019

Basically if the type of what you want to inject is a class then you can avoid to use @Inject, otherwise use it 😃

1reaction
bhaidarcommented, Mar 22, 2019

Oh great! Symbol reminds of InjectionToken.

Thanks @adrien2p a lot! I am paving my way through nestjs still.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 4: When and why is @Inject is used in constructor?
@Inject() is a manual mechanism for letting Angular know that a parameter must be injected. import { Component, Inject } from ...
Read more >
A quick intro to Dependency Injection - freeCodeCamp
So, transferring the task of creating the object to someone else and directly using the dependency is called dependency injection. 1*TF- ...
Read more >
Inject (Java EE 6 ) - Oracle Help Center
Identifies injectable constructors, methods, and fields. May apply to static as well as instance members. An injectable member may have any access modifier...
Read more >
Why Angular 14's new inject() function is so amazing?
The inject() function takes an InjectionToken as a parameter and returns the value for that InjectionToken from the currently active injector. Basically, it's ......
Read more >
When to use Dependency Injection - Jenkov.com
When to use Dependency Injection · You need to inject configuration data into one or more components. · You need to inject the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found