Advanced topics
Controlling visibility
You can be more specific where you want to get dependencies from. For instance, you can ask for another directive on the same element.
class CustomInputComponent { constructor(@Self() f: FormatterDirective) {} }
Or you can ask for a directive in the same template, that is, you can only inject an ancestor directive from the same HTML file.
class CustomInputComponent { constructor(@Host() f: CustomForm) {} }
Finally, you can ask to skip the current element, which can be handy for decorating existing providers or building up tree-like structures.
class SomeComponent { constructor(@SKipSelf() ancestor: SomeComponent) {} }
Optional dependencies
To mark a dependency as optional, use the Optional
decorator.
class Login { constructor(@Optional() service: LoginService) {} }
More on registering providers
Passing a class into an array of providers is the same as using a provider with useClass
, that is, the following two examples are identical:
@NgModule({ providers...