• Overview
@angular/core

inject

function

Injects a token from the currently active injector. inject is only supported in an injection context. It can be used during:

  • Construction (via the constructor) of a class being instantiated by the DI system, such as an @Injectable or @Component.
  • In the initializer for fields of such classes.
  • In the factory function specified for useFactory of a Provider or an @Injectable.
  • In the factory function specified for an InjectionToken.
  • In a stackframe of a function call in a DI context
  
    
  
  
function inject<T>(token: ProviderToken<T>): T;
@paramtokenProviderToken<T>

A token that represents a dependency that should be injected.

@returnsT
function inject<T>(token: ProviderToken<T>, flags?: InjectFlags | undefined): T | null;
@deprecated

prefer an options object instead of InjectFlags

@paramtokenProviderToken<T>

A token that represents a dependency that should be injected.

@paramflagsInjectFlags | undefined

Control how injection is executed. The flags correspond to injection strategies that can be specified with parameter decorators @Host, @Self, @SkipSelf, and @Optional.

@returnsT | null
function inject<T>(token: ProviderToken<T>, options: InjectOptions & { optional?: false | undefined; }): T;
@paramtokenProviderToken<T>

A token that represents a dependency that should be injected.

@paramoptionsInjectOptions & { optional?: false | undefined; }

Control how injection is executed. Options correspond to injection strategies that can be specified with parameter decorators @Host, @Self, @SkipSelf, and @Optional.

@returnsT
function inject<T>(token: ProviderToken<T>, options: InjectOptions): T | null;
@paramtokenProviderToken<T>

A token that represents a dependency that should be injected.

@paramoptionsInjectOptions

Control how injection is executed. Options correspond to injection strategies that can be specified with parameter decorators @Host, @Self, @SkipSelf, and @Optional.

@returnsT | null
function inject(token: HostAttributeToken): string;
@paramtokenHostAttributeToken

A token that represents a static attribute on the host node that should be injected.

@returnsstring
function inject(token: HostAttributeToken, options: { optional: true; }): string | null;
@paramtokenHostAttributeToken

A token that represents a static attribute on the host node that should be injected.

@paramoptions{ optional: true; }
@returnsstring | null
function inject(token: HostAttributeToken, options: { optional: false; }): string;
@paramtokenHostAttributeToken

A token that represents a static attribute on the host node that should be injected.

@paramoptions{ optional: false; }
@returnsstring
Jump to details