Reputation: 21621
Let's say I have a service MyService
@Injectable({
providedIn: 'root',
})
export class LookupsService {
constructor(private readonly store: Store<never>) {}
}
How do I inject it in the following function
export function convertToTreeNodes(
orgs: OrganizationModel[],
service: LookupsService
): OrganizationNodeInterface[] {
//...
}
I am getting an error saying Lookup service was not provided
Thanks for helping
Upvotes: 2
Views: 708
Reputation: 1199
Services can only be injected in something of which lifecycle is maintained by Angular. A normal function exported doesn't have a lifecycle so angular can't inject any dependencies. You have couple of options:
// app.module
export class AppModule {
constructor(private injector: Injector) {
ServiceLocator.init(injector);
}
}
// blah.ts
export function blah() {
ServiceLocator.get(LookupsService)
}
//service-locator.ts
export class ServiceLocator {
private injector: Injector;
init(injector) {
this.injector = injector;
}
get(klass) {
return this.injector.get(klass);
}
}
Upvotes: 2