I have this custom class called SortState that represents sorting and paging on a table of records. Now I want to make that class talk to a service that displays a loading indicator. Apparently you can't use regular Angular dependency injection with classes, only with components. The answers I've found for working around this generally say to use something called ReflectiveInjector, which seems to have been replaced with Injector.
So I tried something like this:
export class SortState {
private loader: LoadingService;
constructor(state: Partial<SortState>) {
const injector = Injector.create({providers: [{provide: LoadingService, deps: []}]});
this.loader = injector.get(LoadingService);
...
}
...
}
And it sort of seems to work, but I think it's creating its own instance of LoadingService, and I need it to use a singleton. So I figure maybe I need to use the providers array in the main AppModule, but I haven't been able to figure out the syntax to make that work.
Injectorwhen instantiating yourSortStateclass.