parent
df4254ae89
commit
54b41f57be
@ -6,7 +6,6 @@
|
|||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {stringify} from '../facade/lang';
|
|
||||||
import {makeParamDecorator} from '../util/decorators';
|
import {makeParamDecorator} from '../util/decorators';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -250,8 +249,7 @@ export const SkipSelf: SkipSelfDecorator = makeParamDecorator('SkipSelf', []);
|
|||||||
export interface HostDecorator {
|
export interface HostDecorator {
|
||||||
/**
|
/**
|
||||||
* @whatItDoes Specifies that an injector should retrieve a dependency from any injector until
|
* @whatItDoes Specifies that an injector should retrieve a dependency from any injector until
|
||||||
* reaching the
|
* reaching the host element of the current component.
|
||||||
* host element of the current component.
|
|
||||||
* @howToUse
|
* @howToUse
|
||||||
* ```
|
* ```
|
||||||
* @Injectable()
|
* @Injectable()
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {Component, Directive, Host, Inject, Injectable, Optional, ReflectiveInjector, Self, SkipSelf} from '@angular/core';
|
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() {
|
export function main() {
|
||||||
describe('di metadata examples', () => {
|
describe('di metadata examples', () => {
|
||||||
@ -140,26 +140,28 @@ export function main() {
|
|||||||
|
|
||||||
@Directive({selector: 'child-directive'})
|
@Directive({selector: 'child-directive'})
|
||||||
class ChildDirective {
|
class ChildDirective {
|
||||||
|
logs: string[] = [];
|
||||||
|
|
||||||
constructor(@Optional() @Host() os: OtherService, @Optional() @Host() hs: HostService) {
|
constructor(@Optional() @Host() os: OtherService, @Optional() @Host() hs: HostService) {
|
||||||
console.log('os is null', os);
|
// os is null: true
|
||||||
console.log('hs is NOT null', hs);
|
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({
|
@Component({
|
||||||
selector: 'parent-cmp',
|
selector: 'parent-cmp',
|
||||||
providers: [HostService],
|
viewProviders: [HostService],
|
||||||
template: `
|
template: '<child-directive></child-directive>',
|
||||||
Dir: <child-directive></child-directive>
|
|
||||||
`
|
|
||||||
})
|
})
|
||||||
class ParentCmp {
|
class ParentCmp {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app',
|
selector: 'app',
|
||||||
providers: [OtherService],
|
viewProviders: [OtherService],
|
||||||
template: `Parent: <parent-cmp></parent-cmp>`
|
template: '<parent-cmp></parent-cmp>',
|
||||||
})
|
})
|
||||||
class App {
|
class App {
|
||||||
}
|
}
|
||||||
@ -168,7 +170,14 @@ export function main() {
|
|||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
declarations: [App, ParentCmp, ChildDirective],
|
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',
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user