Q:

Passing parameters to constructor of a service using Dependency Injection?

I have custom service class:
@Injectable()

export class CustomService {
  constructor(num: number) {
  }
}

This class is injected in constructor of component like this:
constructor(private cs: CustomService) {
}

But how to pass parameter num to service in constructor described above?
Something like that:
constructor(private cs: CustomService(1)) {
}

I know as solution I can use Fabric pattern, but is there only one way to do that?

A:

If CustomService instances should not be injector singletons, it is:
providers: [{ provide: CustomService, useValue: CustomService }]

...

private cs;

constructor(@Inject(CustomService) private CustomService: typeof CustomService) {
  this.cs = new CustomService(1);
}

If CustomService is supposed be memoized to return singletons for respective parameter, instances should be retrieved through additional cache service:
class CustomServiceStorage {
  private storage = new Map();

  constructor(@Inject(CustomService) private CustomService: typeof CustomService) {}
  get(num) {
    if (!this.storage.has(num))
      this.storage.set(num, new this.CustomService(num));

    return this.storage.get(num);
  }
}

