New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to use inject() for element injector hierarchy #47566
Comments
This works as designed: Not sure what is your real-life use-case, but you could always do: export class AppComponent {
public constructor(private _injector: Injector, service: Service) {
console.log(service); // OK
console.log(inject(Service)); //OK
setTimeout(() => {
console.log(this._injector.get(Service)); //KO
}, 100);
}
} |
Thx @pkozlowski-opensource, It this mentioned somewhere in the documentation ? In my use case, I have objects created outside DI, but they still need to get access to injected services. |
Currently your best option is to define those services on the application level. |
@alxhub I've marked this is a feature request but please let me know if you've got any additional concerns. |
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list. You can find more details about the feature request process in our documentation. |
It's possible to create an environment injector that inherits from the node injector, would it be possible to widen the type signature so that @Component({
providers: [Service]
})
class AppComponent {
constructor(parent: Injector) {
const injector= createEnvironmentInjector([], parent as EnvironmentInjector)
setTimeout(() => {
injector.runInContext(() => {
console.log(inject(Service)); // now it works
});
}, 100);
}
} |
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. |
+1 for component |
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. |
Is this issue still open? If yes, Can you assign it to me? |
Fixed by #49396, we can now use |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
No
Description
When providing a Service in a component,
inject()
triggersNullInjectorError
when called withinrunInContext
:This does not happen
providedIn:'root'
Please provide a link to a minimal reproduction of the bug
https://stackblitz.com/edit/angular-ivy-yccqgi?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html
Please provide the environment you discovered this bug in (run
ng version
)The text was updated successfully, but these errors were encountered: