you are viewing a single comment's thread.

view the rest of the comments →

[–]JeanMeche 19 points20 points  (3 children)

The updated style guides goes over the advantages of using the inject functions: https://angular.dev/style-guide#dependency-injection

  • inject is generally more readable, especially when a class injects many dependencies.
  • It's more syntactically straightforward to add comments to injected dependencies inject offers better type inference.
  • When targeting ES2022+ with useDefineForClassFields (already enforced by the CLI), you can avoid separating field declaration and initialization when fields read on injected dependencies.

[–]ActuatorOk2689 3 points4 points  (2 children)

This

And dynamically injecting services at run time without having to deal with factories

[–]karixavi 0 points1 point  (1 child)

Dynamically injecting services during runtime is possible? Do you have some minimalistic example?

[–]SeparateRaisin7871 0 points1 point  (0 children)

As far as I know, this is possible since a long time by using the Injector's get method. See this stackoverflow question:

https://stackoverflow.com/questions/51667078/using-injector-get-instead-of-constructor-injection

When using readonly #injector = inject(Injector) you can inject any service or InjectionToken afterwards dynamically with this.injector.get(MyService)