1

I'm a longtime Angular dev now trying to learn the standalone version. How do I inject a service into another service without a providers array in the NgModule. Is my only choice using providedIn in the Injectable declaration?

4
  • Check this link: angular.dev/guide/di/… If you would like to use injection context, read this: angular.dev/guide/di/… Commented Aug 8 at 9:12
  • I've found this guide useful about standalone in general: v17.angular.io/guide/… You can inject your service if not providedIn: 'root' in: component's providers array, a route providers array or the main bootstrapApplication providers array AFAIK
    – davidlj95
    Commented Aug 9 at 16:21
  • 1
    Thanks @GiancarloSotelo I will review these links.
    – emery.noel
    Commented Aug 9 at 20:35
  • 1
    And @davidlj95 too!
    – emery.noel
    Commented Aug 9 at 20:35

1 Answer 1

0

If your service is not providedIn: root, then you must be adding the service to the providers array of some standalone component (in-order to use it). You need to add the second service to the same array, so that the service has its own instance of the second service.

import { Component, inject } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { FirstService } from './app/first.service';
import { SecondService } from './app/second.service';

@Component({
  selector: 'app-root',
  standalone: true,
  providers: [FirstService, SecondService],
  template: `
    <h1>Hello from {{ name }}!</h1>
    <a target="_blank" href="https://angular.dev/overview">
      Learn more about Angular
    </a>
  `,
})
export class App {
  name = 'Angular';
  firstService = inject(FirstService);
}

bootstrapApplication(App);

Above method is the best approach, but you have other options also.

Inherit the second service.

First Service:

import { Injectable } from '@angular/core';
import { SecondService } from './second.service';

@Injectable()
export class FirstService extends SecondService {
  constructor(protected http: HttpClient) {
    this.test(http);
  }
}

Second Service:

import { Injectable } from '@angular/core';

@Injectable()
export class SecondService {
  constructor(protected http: HttpClient) {}

  test() {
    console.log('from second service');
  }
}

Initialize the second service as a traditional class

You will have to access the second service as a class and only through the first service, it is not visible in angular DI, and you will have to provider the dependencies to the service (E.g.: HttpClient)

import { Injectable } from '@angular/core';
import { SecondService } from './second.service';

@Injectable()
export class FirstService {
  secondService!: SecondService;
  constructor(protected http: HttpClient) {
    this.secondService = new SecondService(http); // <- create a new instance
    // this exists only inside first service.
  }
}
2
  • I am getting errors when adding my service to a component's providers array unless I use providedIn. This is what I found so confusing. I just reproduced it right now by removing providedIn from a service, I'm immediately presented with NullInjectorError: NullInjectorError: No provider for _xService! Besides which, I'm trying to inject "xservice" into "yservice" and getting the same result (when including yservice in the component's provider array)
    – emery.noel
    Commented Aug 9 at 20:37
  • @emery.noel please replicate the exact error on stackblitz with the minimal code, so that I can check what went wrong? Commented Aug 10 at 14:40