AI Hints
Home›Angular Tutorial›@Service and @Injectable, Sharing State in a Service and Signals in Services
Angular · Lesson 67 of 120

@Service and @Injectable

Two ways to declare.

⏱ 2 min read▶ Video at 1:19:12🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Our basket used the service decorator. Lots of existing Angular code uses another decorator, called injectable. You'll see both, so let's compare them.

  • @Service in new code
  • @Injectable in older code
  • Compare them

Two ways to declare a service

The service decorator is new, and short. It's provided automatically, with one instance for the whole app. Injectable, with provided in root, does exactly the same job. It's the older, longer way, and it still works.

@Service()

  • new and shortprovided automaticallyone for the app

@Injectable({ providedIn: 'root' })

  • older and longerprovided in rootone for the app

greeter.ts

This greeter uses injectable, with provided in root. A counter outside the class counts how many greeters are made. Each greeter remembers its number, and says it in hello.

greeter.tstypescript
import { Injectable } from '@angular/core';

let made = 0;

@Injectable({ providedIn: 'root' })
export class Greeter {
  readonly number = ++made;

  hello(name: string) {
    return `Hello, ${name}! (from greeter number ${this.number})`;
  }
}

One greeter for everyone

Two hello components inject the greeter. If each one got a new greeter, we'd see number one, and number two. Run it. Both say number one. There's only one greeter, shared by the whole app.

hello.tstypescript
import { Component, inject, input } from '@angular/core';
import { Greeter } from './greeter';

@Component({
  selector: 'app-hello',
  template: `<p class="card">{{ greeter.hello(name()) }}</p>`,
})
export class Hello {
  name = input('');
  greeter = inject(Greeter);
}
Also uses greeter.ts
import { Injectable } from '@angular/core';

let made = 0;

@Injectable({ providedIn: 'root' })
export class Greeter {
  readonly number = ++made;

  hello(name: string) {
    return `Hello, ${name}! (from greeter number ${this.number})`;
  }
}
Also uses app.ts
import { Component } from '@angular/core';
import { Hello } from './hello';

@Component({
  selector: 'app-root',
  imports: [Hello],
  template: `<app-hello name="Sam" /><app-hello name="Alex" />`,
})
export class App {}

Forgetting providedIn

Here, injectable has no provided in root. So Angular doesn't know where to create it. Run it. The page fails, with error NG zero two zero one. No provider found for greeter.

greeter.tstypescript
import { Injectable } from '@angular/core';

let made = 0;

@Injectable()
export class Greeter {
  readonly number = ++made;

  hello(name: string) {
    return `Hello, ${name}! (from greeter number ${this.number})`;
  }
}
Output
ERROR ɵNotFound: NG0201: No provider found for `_Greeter`. Source: Environment Injector.
Find more at https://v22.angular.dev/errors/NG0201
ɵNotFound: NG0201: No provider found for `_Greeter`. Source: Environment Injector. Find
more at https://v22.angular.dev/errors/NG0201
Also uses hello.ts
import { Component, inject, input } from '@angular/core';
import { Greeter } from './greeter';

@Component({
  selector: 'app-hello',
  template: `<p class="card">{{ greeter.hello(name()) }}</p>`,
})
export class Hello {
  name = input('');
  greeter = inject(Greeter);
}
Also uses app.ts
import { Component } from '@angular/core';
import { Hello } from './hello';

@Component({
  selector: 'app-root',
  imports: [Hello],
  template: `<app-hello name="Sam" /><app-hello name="Alex" />`,
})
export class App {}

Summary

The service decorator, and injectable with provided in root, both give one shared instance. Without provided in root, injectable needs a provider. Next, sharing state in a service.

Key takeaways
  • @Service()
  • @Injectable({ providedIn: 'root' })
  • NG0201: no provider
Next up: Sharing State in a Service — one source of truth