Cookies concent notice

This site uses cookies from Google to deliver its services and to analyze traffic.
Learn more
Skip to main content
Angular has a new websiteHead 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

Parameter decorator on a dependency parameter of a class constructor that specifies a custom provider of the dependency.

OptionDescription
token

A DI token that maps to the dependency to be injected.

See also

Options

A DI token that maps to the dependency to be injected.

      
      token: any
    

Usage notes

The following example shows a class constructor that specifies a custom provider of a dependency using the parameter decorator.

When @Inject() is not present, the injector uses the type annotation of the parameter as the provider.

      
      class Engine {}

@Injectable()
class Car {
  constructor(public engine: Engine) {} // same as constructor(@Inject(Engine) engine:Engine)
}

const injector = Injector.create({
  providers: [
    {provide: Engine, deps: []},
    {provide: Car, deps: [Engine]},
  ],
});
expect(injector.get(Car).engine instanceof Engine).toBe(true);