I have an InjectionToken
like:
export const SOURCE_DATA_INJECTION_TOKEN =
new InjectionToken<CustomObject[]>('All Source Data', {
providedIn: 'root',
factory: () => {
// Returns all source
return someArrayData // This data is read from a local .json file;
}
});
And my component uses it directly as it should,
@Component({})
export class SourceManager {
sourceDataList: CustomObject[] = inject(SOURCE_DATA_INJECTION_TOKEN);
}
Now, I have to upgrade my INJECTION_TOKEN to return data based on the source selected by the user in Component. Updates in the component will look like this:
@Component({})
export class SourceManager {
selectedSource: SourceEnum = SourceEnum.SomeSource1
sourceDataList: CustomObject[] = inject(SOURCE_DATA_INJECTION_TOKEN);
}
This selectedSource
can be changed from UI. I want to send this selectedSource
enum to my injection token and instead of returning all sources, now the token will be returning filtered data.
I see factory method can have parameter as well, can I somehow pass selectedSource
to it?