Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(Host): fix the API example #11684

Merged
merged 1 commit into from Sep 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions modules/@angular/core/src/di/metadata.ts
Expand Up @@ -6,7 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/

import {stringify} from '../facade/lang';
import {makeParamDecorator} from '../util/decorators';

/**
Expand Down Expand Up @@ -250,8 +249,7 @@ export const SkipSelf: SkipSelfDecorator = makeParamDecorator('SkipSelf', []);
export interface HostDecorator {
/**
* @whatItDoes Specifies that an injector should retrieve a dependency from any injector until
* reaching the
* host element of the current component.
* reaching the host element of the current component.
* @howToUse
* ```
* @Injectable()
Expand Down
29 changes: 19 additions & 10 deletions modules/@angular/examples/core/di/ts/metadata_spec.ts
Expand Up @@ -7,7 +7,7 @@
*/

import {Component, Directive, Host, Inject, Injectable, Optional, ReflectiveInjector, Self, SkipSelf} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {ComponentFixture, TestBed} from '@angular/core/testing';

export function main() {
describe('di metadata examples', () => {
Expand Down Expand Up @@ -140,26 +140,28 @@ export function main() {

@Directive({selector: 'child-directive'})
class ChildDirective {
logs: string[] = [];

constructor(@Optional() @Host() os: OtherService, @Optional() @Host() hs: HostService) {
console.log('os is null', os);
console.log('hs is NOT null', hs);
// os is null: true
this.logs.push(`os is null: ${os === null}`);
// hs is an instance of HostService: true
this.logs.push(`hs is an instance of HostService: ${hs instanceof HostService}`);
}
}

@Component({
selector: 'parent-cmp',
providers: [HostService],
template: `
Dir: <child-directive></child-directive>
`
viewProviders: [HostService],
template: '<child-directive></child-directive>',
})
class ParentCmp {
}

@Component({
selector: 'app',
providers: [OtherService],
template: `Parent: <parent-cmp></parent-cmp>`
viewProviders: [OtherService],
template: '<parent-cmp></parent-cmp>',
})
class App {
}
Expand All @@ -168,7 +170,14 @@ export function main() {
TestBed.configureTestingModule({
declarations: [App, ParentCmp, ChildDirective],
});
expect(() => TestBed.createComponent(App)).not.toThrow();

let cmp: ComponentFixture<App>;
expect(() => cmp = TestBed.createComponent(App)).not.toThrow();

expect(cmp.debugElement.children[0].children[0].injector.get(ChildDirective).logs).toEqual([
'os is null: true',
'hs is an instance of HostService: true',
]);
});
});

Expand Down