Deepak Verma
Explain host decorator in Angular 2.
By Deepak Verma in Angular on May 31 2018
  • Shivam Shukla
    Jun, 2018 27

    @Host — The @Host decorator tells DI to look for a dependency in any injector until it reaches the host@Host example (source) class OtherService {} class HostService {}@Directive({selector: 'child-directive'}) class ChildDirective {logs: string[] = [];constructor(@Optional() @Host() os: OtherService, @Optional() @Host() hs: HostService) {// os is null: truethis.logs.push(`os is null: ${os === null}`);// hs is an instance of HostService: truethis.logs.push(`hs is an instance of HostService: ${hs instanceof HostService}`);} }@Component({selector: 'parent-cmp',viewProviders: [HostService],template: '', }) class ParentCmp { }@Component({selector: 'app',viewProviders: [OtherService],template: '', }) class App { }

    • 1

Most Popular Job Functions