I'm trying to provide a service in the following way:
my-module.module.ts
import { NgModule } from '@angular/core';
import { MyService} from './my-service.service';
@NgModule({
providers: [
{
provide: MyService,
useFactory: () => new MyService(2),
},
],
})
export class MyModule {}
my-service.service.ts
import { Inject, Injectable } from '@angular/core';
@Injectable()
export class MyService {
constructor(
private n: number
) {
console.log(this.n);
}
}
But I receive the following error:
error NG2003: No suitable injection token for parameter 'n' of class 'MyService'. Consider using the @Inject decorator to specify an injection token.
There is this workaround, to use Inject(''), but this seems weird and is not mentioned in the documentation:
my-service.service.ts
import { Inject, Injectable } from '@angular/core';
@Injectable()
export class MyService {
constructor(
// weird fix
@Inject('') private n: number
) {
console.log(this.n);
}
}
The Angular version that I'm using is 13.3.11