I am creating a generic crud service that i want to use throughout my angular application in various feature modules.
To achieve this i need to pass a string value to a service in providers: of an angular module, so i can then set a value to the super of the base service. Is this possible?
Config
import { InjectionToken } from '@angular/core';
export const COLLECTION_REF = new InjectionToken<string>('collection');
Module
import { GenericEntityService } from '@mifowi/core/services/genericEntity.service';
import { COLLECTION_REF } from './orgModuleConfig';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
@NgModule({
....
**I want to pass in a string value of 'Organization' to the GenericEntityService**
providers: [
GenericEntityService,
{ provide: COLLECTION_REF, useValue: 'Organization' }
],
})
export class OrganizationsModule {}
Generic Service
import { FirestoreService } from './firestore.service';
import { Injectable } from '@angular/core';
import {
EntityCollectionServiceBase,
EntityCollectionServiceElementsFactory
} from '@ngrx/data';
import { Observable } from 'rxjs';
@Injectable()
export class GenericEntityService extends EntityCollectionServiceBase<any> {
constructor(
@Inject(COLLECTION_REF) private collection: string,
public firestoreService: FirestoreService,
elementsFactory: EntityCollectionServiceElementsFactory
) {
**I want to set the string below to
the value passed in providers in the module - see above**
super(collection, elementsFactory);
}
getMyEntityList(entity: string, id: string): Observable<any[]> {
return this.firestoreService.getMyEntityList(entity, id);
}
getEntityByName(entity: string, str: string): Observable<any[]> {
return this.firestoreService.colWithName$(entity, str);
}
createNewEntity(entity: string, payload: any): void {
this.firestoreService.createDoc(entity, payload);
}
updateEntity(entity: string, payload: any): void {
this.firestoreService.updateDoc(entity, payload);
}
deleteEntity(entity: string, payload: any): void {
this.firestoreService.deleteDoc(entity, payload);
}
}