HostMetadata

export class HostMetadata

exported from angular2/core defined in angular2/src/core/di/metadata.ts (line 180)

Specifies that an injector should retrieve a dependency from any injector until reaching the closest host.

In Angular, a component element is automatically declared as a host for all the injectors in its view.

Example (live demo)

In the following example App contains ParentCmp, which contains ChildDirective. So ParentCmp is the host of ChildDirective.

ChildDirective depends on two services: HostService and OtherService. HostService is defined at ParentCmp, and OtherService is defined at App.

class OtherService {} class HostService {} @Directive({ selector: 'child-directive' }) class ChildDirective { constructor(@Optional() @Host() os:OtherService, @Optional() @Host() hs:HostService){ console.log("os is null", os); console.log("hs is NOT null", hs); } } @Component({ selector: 'parent-cmp', providers: [HostService], template: ` Dir: <child-directive></child-directive> `, directives: [ChildDirective] }) class ParentCmp { } @Component({ selector: 'app', providers: [OtherService], template: ` Parent: <parent-cmp></parent-cmp> `, directives: [ParentCmp] }) class App { } bootstrap(App);

Members

toString() : string

Not Yet Documented