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.
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
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})`; }}
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 {}
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 {}
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.