Questions tagged [injection-tokens]
19 questions
6
votes
0 answers
Angular with Jest: failing test for not being able to find provider using different import
I have an Angular app with Jest and all tests are working just fine.
A component injects a service:
component.ts:
import { FooService } from '../../services/foo.service'; // relative import
constructor(private foo: FooService)…

Felix
- 3,999
- 3
- 42
- 66
5
votes
1 answer
inject in standalone component fails if moved to an npm library
I created a simple angular standalone component that uses an injection token for configuration:
export const PERSON_DEFAULT_OPTIONS = new InjectionToken('...')
export interface IPersonDefaults { ... }
export const…

Gargoyle
- 9,590
- 16
- 80
- 145
3
votes
1 answer
Angular: HttpInterceptor declared in library module intercepting requests outside of library
I have an issue in Angular whereby an implemented instance of HttpInterceptors declared in an Angular library is intercepting requests for HttpClient calls made outside the library (i.e. the consuming application).
I am struggling to understand why…

keldar
- 6,152
- 10
- 52
- 82
2
votes
0 answers
How can I register my own injection providers globally to work around circular module dependencies?
We can use Angular's @Injectable decorator to make a service available for dependency injection throughout the application. In this case, the service class itself will be used as the injection token; whoever injects it must know the class.
We can…

F-H
- 663
- 1
- 10
- 21
2
votes
1 answer
Angular use different injection token conditionally in module from routing module
I want to use different value for a injection token in a module conditionally, something like:
providers: [
{
provide: ABC_TOKEN,
useValue: useA ? a : b,
},
],
how to do that?
and also I want to pass the useA from a routing…

kk-at-cbx
- 329
- 3
- 10
2
votes
1 answer
Provide an InjectionToken after APP_INITIALIZATION with the resolved configuration
I need to get the config using a factory that will be resolved during the APP INITIALIZATION (using the APP_INITIALIZER provider).
export function loadConfig(): () => Promise {
// return promised config
}
That is provided using the…

frankfullstack
- 498
- 5
- 18
2
votes
0 answers
Angular DI Injection token's type does not match a factory's return value type but service accepts
Why is it possible to provide an injection token of some type but can pass an actual value of a differing type and this be accepted in a service injected with the token?
import { InjectionToken } from '@angular/core';
export const CONFIG = new…

DJ. Y
- 21
- 2
1
vote
2 answers
Angular App initialization with Observables
I have this application which needs some initialization before starting.
I'm using the recommended way, with the APP_INITIALIZER, the factory function, etc. along with the syntax in the factory function that allows the use of observables instead of…

zeroquaranta
- 392
- 4
- 16
1
vote
1 answer
Configure a external Angular lib with .forRoot() that depends on a main Application service for populate config values
I have a custom Library that needs custom configuration within the forRoot() Library module, but I need to use an App Config Module that loads that configuration asynchronously instead of using static injected data as following.
// Custom Library…

frankfullstack
- 498
- 5
- 18
1
vote
1 answer
How to pass injection token to nested service using SCAM approach (Single Component Angular Module)?
I have been playing with SCAM (Single Component Angular Modules) approach and I am facing a problem passing injection token from parent to child module.
I have a ScamService with httpClient and baseUrl injected:
import { HttpClient, HttpClientModule…

mgb
- 11
- 1
1
vote
0 answers
Is it possible to change provider of component in Angular Material Dialog?
I have a component with InjectionToken and custom provider to get data from store. In base usage of the component I need to provide data with "user logins", but in dialog I need to get "user names" with the same component. Is it possible to pass…

fiorsaoirse
- 26
- 2
0
votes
1 answer
Angular - InjectionToken with Parameter
I have an InjectionToken like:
export const SOURCE_DATA_INJECTION_TOKEN =
new InjectionToken('All Source Data', {
providedIn: 'root',
factory: () => {
// Returns all source
return someArrayData // This…

itsji10dra
- 4,603
- 3
- 39
- 59
0
votes
0 answers
Injecting separate configuration for a generic Angular service
I have a generic data service which has its configuration injected as per below:
export default class GenericDataService implements IGenericDataService {
constructor(private readonly httpClient: HttpClient,
private readonly config:…

atamata
- 997
- 3
- 14
- 32
0
votes
0 answers
No provider for DialogRef InjectionToken
I am creating a directive that opens JavaScript's confirm or CDK Dialog based on condition, CDK dialog is optional, and when not used I want it to be tree shaken.
So I used InjectionToken to get the reference of CDK dialog and dialogRef.
/**…

Sameer
- 4,758
- 3
- 20
- 41
0
votes
0 answers
How to provide an injectiontoken to an injectable service
I have a module with a service that I am sharing
@Injectable({providedIn: 'root'})
export class SharedService{
constructor(
private store: Store,
@Inject(API_REQUESTS) private apiRequests: ApiRequestInterface[]
) {
…

Nealv
- 6,856
- 8
- 58
- 89