Class Central is learner-supported. When you buy through links on our site, we may earn an affiliate commission.

egghead.io

Angular Service Injection with the Dependency Injector (DI)

via egghead.io

Overview

Services and registering service providers are an inherent part of an Angular application. It is where you should define your application logic, they keep the state of the application and allow to share that among different components.
That said, components can ask for instances of services by specifying them as parameters of their constructor functions which will then provided at runtime by Angular’s dependency injection mechanism. While services themselves are mostly just plain ES2015 classes, there are some interesting things to discover, especially when it comes to dependency injection.
We can define services at various levels within our application, which allows us to control the scope and visibility of a given service. We can furthermore alias old service definitions to new, existing services which is handy for refactoring scenarios and we also have the possibility to define so-called factory functions for fully taking control of how a given service is being constructed.
In this course we will take a closer look and walk step by step through all of these scenarios.

Syllabus

  • Introducing Angular Service Injection with the Dependency Injector
  • Define an Angular Service
  • Register a Service to Angular’s Dependency Injection Mechanism
  • Create Multiple Angular Service Instances
  • Define an Aliased Class Provider in Angular
  • Simplify an Angular Service using a JavaScript Object as a Value Provider
  • Instantiate a Service using an Angular Factory Provider
  • Pass Dependencies to a Factory Provider in Angular
  • Overwrite a Service in a Component Subtree in Angular
  • Use an Abstract Class as DI Token for your Services in Angular
  • Control the dependency lookup with @Host, @SkipSelf and @Optional
  • Avoid Multiple Service Instances in Lazy Loaded Modules in Angular
  • Use "providedIn" service registration to avoid multiple instances with lazy loaded modules
  • Use providedIn for the Angular Service registration to reduce bundle size

Taught by

Juri Strumpflohner

Reviews

4.6 rating at egghead.io based on 280 ratings

Start your review of Angular Service Injection with the Dependency Injector (DI)

Never Stop Learning.

Get personalized course recommendations, track subjects and courses with reminders, and more.

Someone learning on their laptop while sitting on the floor.