I’m learning Angular right now – as a React.js fangirl.

Pluralsight offers a free month of learning in April. I’m taking advantage of it.

Here are some notes on the course Angular Services by Brice Wilson.

Angular Services

What and Why?

Creating and Using Services

  • service = basically a TypeScript class
  • @Injectable decorator
  • Provider required

Example Service:

@Injectable
export class SomeService {
  // some methods
}

Providing a Service:

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule],
  bootstrap: [AppComponent],
  providers: [SomeService],
})
export class AppModule {}
  • providers are like recipes
  • provider hands service to injector (creates a single instance of the service)
  • Angular uses constructor injection
  • @Injectable({providedIn: 'root'}) is tree-shakable

Dependency Injection

Asynchronous Services

  • can contain [synchronous and asynchronous code][whatcolor]
  • return Observables (part of Rxjs or Promises (ES 2015)

Using Built-In Angular Services

Summary

The course offers several demos that help you understand how Angular services work. Those are really useful, but I cannot include them here.

Resources