educative.io

Use services without @Injectable() decorator

I know if we use the @Injectable() decorator with “providedIn: ‘root’” property we don’t have to point it in the module “providers” and in this case we can use it throughout the application.

I also know that if we place a service inside the module “providers” for it we can use it throughout the app as well and we don’t have use @Injectable() decorator.

But I saw many times how people use it without providedIn, just @Injectable() and place it inside the module “providers”.

Do the @injectable() usage redundant in such case?

// client.module.ts
@NgModule({
  ...
  providers: [ClientService]
  ...
})
export class ClientModule {}
// client.service.ts
export class ClientService {
  items = [1, 2, 3]

  constructor() { }

  extendItems(val: number) {
    this.items.push(val);
  }
}

Course: The Angular Masterclass - Learn Interactively
Lesson: The Hierarchy of Injectors - The Angular Masterclass