Help Angular by taking a 1 minute survey!Go to surveyHome

Optional

A parameter decorator to be used on constructor parameters, which marks the parameter as being an optional dependency. The DI framework provides null if the dependency is not found.

See more...

Description

Can be used together with other parameter decorators that modify how dependency injection operates.

Learn more in the "Dependency Injection Guide".

Options

Usage notes

The following code allows the possibility of a null result:

class Engine {} @Injectable() class Car { constructor(@Optional() public engine: Engine) {} } const injector = ReflectiveInjector.resolveAndCreate([Car]); expect(injector.get(Car).engine).toBeNull();
      
      class Engine {}

@Injectable()
class Car {
  constructor(@Optional() public engine: Engine) {}
}

const injector = ReflectiveInjector.resolveAndCreate([Car]);
expect(injector.get(Car).engine).toBeNull();