Category : angular-providers

Consider the following snippet. How do I access the provided value of Environment in forRoot? Is there an alternate way that I do the same? interface Environment { tokens: Record<string, string>; } const ENVIRONMENT = new InjectionToken<Environment>(‘ENVIRONMENT’); // `SomeModule` with `forRoot` to configure it. This module is from an external library. // Some library module ..

Read more

Consider the following snippet. How do I access the provided value of Environment in forRoot? Is there an alternate way that I do the same? interface Environment { tokens: Record<string, string>; } const ENVIRONMENT = new InjectionToken<Environment>(‘ENVIRONMENT’); // `SomeModule` with `forRoot` to configure it. This module is from an external library. // Some library module ..

Read more

In value-based providers (tokens), we have something like this (in the module): const CONFIG = ‘CONFIG’; providers: [ { provide: CONFIG, useValue: myConfigObj, }, Because we defined the provider in module, in a service we can do this: constructor(@Optional() @Inject(CONFIG) config) { console.log(config); } But when we want to access PLATFORM_ID like this: constructor(@Inject(PLATFORM_ID) private ..

Read more

export const FormatDate = new InjectionToken(‘FormatDate’); … @Component({ selector: ‘…’, templateUrl: ‘…’, styleUrls: [‘…’], providers: [{ provide: FormatDate, useValue: ‘MM.yyyy’ }] }) export class DatePicker { @Input() format: string … } I want to dynamically provide ‘FormatDate’ based on input ‘format’ parameter. If format === ‘short’ then useValue: ‘MM.yy’. If format === ‘long’ then useValue: ..

Read more

I’m trying to create a authorization library within Angular A basic example would be this abstract class which will be in the authorization library: @Injectable({ providedIn: ‘root’, }) export abstract class AuthService { abstract isAuthenticated(): boolean; } Within the library I’m creating a guard and I want that guard to use the authorization service : ..

Read more