Total Guide To Angular 6+ Dependency Injection — providedIn vs providers:[ ] 💉

Tomas Trajan
13 min readNov 6, 2018
Original 📸 by Modestas Urbonas

🤫 Psst! Do you think that NgRx or Redux are overkill for your needs? Looking for something simpler? Check out @angular-extensions/model library!

Try out @angular-extensions/model library! Check out docs & Github

I know, I know… Angular 7 is out already but this topic is as relevant as ever! Angular 6 brought us new better providedIn syntax for registration of services into Angular dependency injection mechanism.

As it turned out, this topic can evoke quite emotional responses and there is a lot of confusion across GitHub comments, Slack and Stack Overflow so let’s make this clear once and for all!

📖 What we’re going to learn

  1. Dependency Injection (DI) recapitulation (optional😉)
  2. The Old Way™ of doing DI in Angular — providers: []
  3. The New Way™ of doing DI in Angular — providedIn: 'root' | SomeModule
  4. Possible scenarios when using providedIn
  5. Recommendation on how to use new syntax in your projects
  6. Summary

💉 Dependency Injection

--

--

Tomas Trajan
Tomas Trajan

Written by Tomas Trajan

👋 I build, teach, write & speak about #Angular & #NgRx for enterprises 👨‍💻 Google Developer Expert #GDE 👨‍🏫 @AngularZurich meetup co-organizer