Jon Sud
Jon Sud

Reputation: 11661

Why angular have DI?

Why Angular have services in dependency injection idea?

I know what is dependency injection is. but why not just create a ts file with import and export (which are singleton by default).

Which cases the idea of create ts file didn't cover?

Upvotes: 0

Views: 34

Answers (1)

Poul Kruijt
Poul Kruijt

Reputation: 71931

I believe the guide and cook book are already pretty self explanatory. Things you do not have with just ts:

  • Non singletons, a provider for every component/module instance when provided in the component's/module's provider array (limit scope to sub-tree)
  • Mock providers while testing
  • Override providers when wanting to change or increase functionality of provider
  • Provide tokens while maintaining type safety
  • Optional providers
  • Multiple providers with one injection
  • Overcome circulair injects with forwardRef

Anyways, if you think you can just use simple ts imports to get what you need, you can of course use it. Angular is aimed to give an opinionated way on how to structure your application and code. This is particularly useful in an enterprise environment when you are working with multiple developers, so that somebody who is proficient with Angular can easily familiarize himself with the new source.

If you are just working alone, you are free to do what you want :)

Upvotes: 1

Related Questions