Description
Describe the problem that you experienced
The documentation on InjectionToken
suggests that this code snippet will work in a meaningful way:
interface MyInterface {...}
const myInterface = injector.get(new InjectionToken<MyInterface>('SomeToken'));
However, trying this shows that it cannot work: If the InjectionToken
instance passed to injector.get
is not the same as the one specified in the provider, Angular will throw a NullInjectorException
.
Therefore, passing a newly constructed InjectionToken
instance to injector.get
should never make any sense, as it will invariably lead to the described exception. At least, this seems to be the case when the token is typed to a type that does not have a runtime representation, such as an interface - but note that precisely this case is what the example, and the text blocks around it, are all about.
Enter the URL of the topic with the problem
https://angular.io/api/core/InjectionToken
Describe what you were looking for in the documentation
I am in a situation where I need to provide a generically typed interface implementation on one side, and inject this very interface, generically typed, on the other side (e.g. of a component outlet).
To get the injection token with the correct type, I had a factory object generate the InjectionToken
instance with the correct type parameter from a function in both the provider and the injection call.
I was looking for confirmation that this can work, and the example showing that I can pass a newly instantiated InjectionToken
to injector.get
seemed to confirm this.
Describe the actions that led you to experience the problem
I ran a little experiment, by declaring a provider, passing it to a component outlet, and trying to inject that provided value.
When I called the InjectionToken
constructor (e.g. like this: new InjectionToken<ITestInfo>('test-info')
, with ITestInfo
being a commonly accessible interface) both in the provider and the inject call (with the same type argument and desc value), I would get a NullInjectorException
.
When I used one and the same InjectionToken
in both places, on the other hand, this worked without errors.
Describe what you want to experience that would fix the problem
- A clear statement that, when typing the injection token to an interface type (or presumably, at least any type without a runtime representation), using the same instance of
InjectionToken
in both provider and injection call is crucial. - A change of the misleading example to make clear that the same instance of
InjectionToken
has to be passed toinjector.get
as what was used to provide the value.
Add a screenshot if that helps illustrate the problem
No response
If this problem caused an exception or error, please paste it here
ERROR NullInjectorError: R3InjectorError(ModulesExampleTopicOneModule)[InjectionToken test-info -> InjectionToken test-info -> InjectionToken test-info]:
NullInjectorError: No provider for InjectionToken test-info!
### If the problem is browser-specific, please specify the device, OS, browser, and version
_No response_
### Provide any additional information here in as much as detail as you can
_No response_