Cookies concent notice

This site uses cookies from Google to deliver its services and to analyze traffic.
Learn more
Skip to main content
This site is no longer updated.Head to Angular.devHome
/

This is the archived documentation for Angular v17. Please visit angular.dev to see this page for the current version of Angular.

inject

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
      
      inject<T>(token: HostAttributeToken | ProviderToken<T>, flags: InjectOptions | InjectFlags = InjectFlags.Default)
    
Parameters
token HostAttributeToken | ProviderToken<T>

A token that represents a dependency that should be injected.

flags InjectOptions | InjectFlags

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

Optional. Default is InjectFlags.Default.

Throws

Error if called outside of a supported context.

Overloads

      
      inject(token: ProviderToken<T>): T
    
Parameters
token ProviderToken<T>

A token that represents a dependency that should be injected.

Returns

T: the injected value if operation is successful, null otherwise.

Throws

Error if called outside of a supported context.

      
      inject(token: ProviderToken<T>, flags?: InjectFlags): T | null
    

Deprecated prefer an options object instead of InjectFlags

Parameters
token ProviderToken<T>

A token that represents a dependency that should be injected.

flags InjectFlags

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

Optional. Default is undefined.

Returns

T | null: the injected value if operation is successful, null otherwise.

Throws

Error if called outside of a supported context.

      
      inject(token: ProviderToken<T>, options: InjectOptions & { optional?: false; }): T
    
Parameters
token ProviderToken<T>

A token that represents a dependency that should be injected.

options InjectOptions & { optional?: false; }

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

Returns

T: the injected value if operation is successful.

Throws

Error if called outside of a supported context, or if the token is not found.

      
      inject(token: ProviderToken<T>, options: InjectOptions): T | null
    
Parameters
token ProviderToken<T>

A token that represents a dependency that should be injected.

options InjectOptions

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

Returns

T | null: the injected value if operation is successful, null if the token is not found and optional injection has been requested.

Throws

Error if called outside of a supported context, or if the token is not found and optional injection was not requested.

      
      inject(token: HostAttributeToken): string
    
Parameters
token HostAttributeToken

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

Returns

string: Value of the attribute if it exists.

Throws

Error If called outside of a supported context or the attribute does not exist.

      
      inject(token: HostAttributeToken, options: { optional: true; }): string | null
    
Parameters
token HostAttributeToken

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

options { optional: true; }
Returns

string | null: Value of the attribute if it exists, otherwise null.

Throws

Error If called outside of a supported context.

      
      inject(token: HostAttributeToken, options: { optional: false; }): string
    
Parameters
token HostAttributeToken

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

options { optional: false; }
Returns

string: Value of the attribute if it exists.

Throws

Error If called outside of a supported context or the attribute does not exist.

Usage notes

In practice the inject() calls are allowed in a constructor, a constructor parameter and a field initializer:

      
      @Injectable({providedIn: 'root'})
export class Car {
  radio: Radio|undefined;
  // OK: field initializer
  spareTyre = inject(Tyre);

  constructor() {
    // OK: constructor body
    this.radio = inject(Radio);
  }
}
    

It is also legal to call inject from a provider's factory:

      
      providers: [
  {provide: Car, useFactory: () => {
    // OK: a class factory
    const engine = inject(Engine);
    return new Car(engine);
  }}
]
    

Calls to the inject() function outside of the class creation context will result in error. Most notably, calls to inject() are disallowed after a class instance was created, in methods (including lifecycle hooks):

      
      @Component({ ... })
export class CarComponent {
  ngOnInit() {
    // ERROR: too late, the component instance was already created
    const engine = inject(Engine);
    engine.start();
  }
}