diff --git a/modules/@angular/compiler/src/metadata_resolver.ts b/modules/@angular/compiler/src/metadata_resolver.ts index 698ff03df1..cb22e95922 100644 --- a/modules/@angular/compiler/src/metadata_resolver.ts +++ b/modules/@angular/compiler/src/metadata_resolver.ts @@ -825,6 +825,8 @@ export class CompileMetadataResolver { token = paramEntry.attributeName; } else if (paramEntry instanceof Inject) { token = paramEntry.token; + } else if (paramEntry instanceof InjectionToken) { + token = paramEntry; } else if (isValidType(paramEntry) && token == null) { token = paramEntry; } diff --git a/modules/@angular/compiler/test/template_parser/template_parser_spec.ts b/modules/@angular/compiler/test/template_parser/template_parser_spec.ts index 79a01d502d..f84554dbdf 100644 --- a/modules/@angular/compiler/test/template_parser/template_parser_spec.ts +++ b/modules/@angular/compiler/test/template_parser/template_parser_spec.ts @@ -827,7 +827,6 @@ Binding to attribute 'onEvent' is disallowed for security reasons (" { + const TOKEN = new InjectionToken('token'); + + const injector = createInjector([ + {provide: TOKEN, useValue: 'by token'}, + {provide: Engine, useFactory: (v: string) => v, deps: [[TOKEN]]}, + ]); + + const engine = injector.get(Engine); + expect(engine).toEqual('by token'); + }); + it('should provide to a factory', () => { - function sportsCarFactory(e: any /** TODO #9100 */) { return new SportsCar(e); } + function sportsCarFactory(e: any) { return new SportsCar(e); } const injector = createInjector([Engine, {provide: Car, useFactory: sportsCarFactory, deps: [Engine]}]); diff --git a/modules/@angular/core/test/linker/view_injector_integration_spec.ts b/modules/@angular/core/test/linker/view_injector_integration_spec.ts index acb46de1ab..3652c485f4 100644 --- a/modules/@angular/core/test/linker/view_injector_integration_spec.ts +++ b/modules/@angular/core/test/linker/view_injector_integration_spec.ts @@ -7,12 +7,11 @@ */ import {USE_VIEW_ENGINE} from '@angular/compiler/src/config'; -import {Attribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, DebugElement, Directive, ElementRef, Host, Inject, Input, Optional, Pipe, PipeTransform, Provider, Self, SkipSelf, TemplateRef, Type, ViewContainerRef} from '@angular/core'; +import {Attribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, DebugElement, Directive, ElementRef, Host, Inject, InjectionToken, Input, Optional, Pipe, PipeTransform, Provider, Self, SkipSelf, TemplateRef, Type, ViewContainerRef} from '@angular/core'; import {ComponentFixture, TestBed, fakeAsync} from '@angular/core/testing'; import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; import {expect} from '@angular/platform-browser/testing/matchers'; - @Directive({selector: '[simpleDirective]'}) class SimpleDirective { @Input('simpleDirective') value: any = null; @@ -107,32 +106,27 @@ class NeedsAttribute { @Directive({selector: '[needsAttributeNoType]'}) class NeedsAttributeNoType { - fooAttribute: any; - constructor(@Attribute('foo') fooAttribute: any) { this.fooAttribute = fooAttribute; } + constructor(@Attribute('foo') public fooAttribute: any) {} } @Directive({selector: '[needsElementRef]'}) class NeedsElementRef { - elementRef: any; - constructor(ref: ElementRef) { this.elementRef = ref; } + constructor(public elementRef: ElementRef) {} } @Directive({selector: '[needsViewContainerRef]'}) class NeedsViewContainerRef { - viewContainer: any; - constructor(vc: ViewContainerRef) { this.viewContainer = vc; } + constructor(public viewContainer: ViewContainerRef) {} } @Directive({selector: '[needsTemplateRef]'}) class NeedsTemplateRef { - templateRef: any; - constructor(ref: TemplateRef) { this.templateRef = ref; } + constructor(public templateRef: TemplateRef) {} } @Directive({selector: '[optionallyNeedsTemplateRef]'}) class OptionallyNeedsTemplateRef { - templateRef: any; - constructor(@Optional() ref: TemplateRef) { this.templateRef = ref; } + constructor(@Optional() public templateRef: TemplateRef) {} } @Directive({selector: '[directiveNeedsChangeDetectorRef]'}) @@ -226,10 +220,17 @@ function createTests({viewEngine}: {viewEngine: boolean}) { // On CJS fakeAsync is not supported... if (!getDOM().supportsDOMEvents()) return; - beforeEach(() => TestBed.configureTestingModule({ - declarations: [TestComp], - providers: [{provide: 'appService', useValue: 'appService'}] - })); + const TOKEN = new InjectionToken('token'); + + beforeEach(() => { + TestBed.configureTestingModule({ + declarations: [TestComp], + providers: [ + {provide: TOKEN, useValue: 'appService'}, + {provide: 'appService', useFactory: (v: string) => v, deps: [TOKEN]}, + ], + }); + }); describe('injection', () => { it('should instantiate directives that have no dependencies', () => { diff --git a/modules/@angular/examples/core/di/ts/provider_spec.ts b/modules/@angular/examples/core/di/ts/provider_spec.ts index 2e964799e3..fce21ac041 100644 --- a/modules/@angular/examples/core/di/ts/provider_spec.ts +++ b/modules/@angular/examples/core/di/ts/provider_spec.ts @@ -137,7 +137,7 @@ export function main() { provide: Hash, useFactory: (location: string) => `Hash for: ${location}`, // use a nested array to define metadata for dependencies. - deps: [[new Optional(), new Inject(Location)]] + deps: [[new Optional(), Location]] }]); expect(injector.get(Hash)).toEqual('Hash for: null');