diff --git a/modules/angular2/src/core/compiler/compiler.js b/modules/angular2/src/core/compiler/compiler.js index 74c9ff3447..c6f50cd453 100644 --- a/modules/angular2/src/core/compiler/compiler.js +++ b/modules/angular2/src/core/compiler/compiler.js @@ -1,4 +1,4 @@ -import {Injectable} from 'angular2/di'; +import {Injectable, Binding} from 'angular2/di'; import {Type, isBlank, isPresent, BaseException, normalizeBlank, stringify} from 'angular2/src/facade/lang'; import {Promise, PromiseWrapper} from 'angular2/src/facade/async'; import {List, ListWrapper, Map, MapWrapper} from 'angular2/src/facade/collection'; @@ -76,9 +76,13 @@ export class Compiler { _bindDirective(directiveTypeOrBinding):DirectiveBinding { if (directiveTypeOrBinding instanceof DirectiveBinding) { return directiveTypeOrBinding; + } else if (directiveTypeOrBinding instanceof Binding) { + let meta = this._reader.read(directiveTypeOrBinding.token); + return DirectiveBinding.createFromBinding(directiveTypeOrBinding, meta.annotation); + } else { + let meta = this._reader.read(directiveTypeOrBinding); + return DirectiveBinding.createFromType(meta.type, meta.annotation); } - var meta = this._reader.read(directiveTypeOrBinding); - return DirectiveBinding.createFromType(meta.type, meta.annotation); } // Create a hostView as if the compiler encountered . diff --git a/modules/angular2/test/core/compiler/integration_spec.js b/modules/angular2/test/core/compiler/integration_spec.js index 4956644516..c8351f87f7 100644 --- a/modules/angular2/test/core/compiler/integration_spec.js +++ b/modules/angular2/test/core/compiler/integration_spec.js @@ -274,6 +274,31 @@ export function main() { }); })); + it('should allow specifying directives as bindings', inject([TestBed, AsyncTestCompleter], (tb, async) => { + tb.overrideView(MyComp, new View({ + template: '', + directives: [bind(ChildComp).toClass(ChildComp)] + })); + + tb.createView(MyComp, {context: ctx}).then((view) => { + view.detectChanges(); + + expect(view.rootNodes).toHaveText('hello'); + async.done(); + }); + })); + + it('should read directives metadata from their binding token', inject([TestBed, AsyncTestCompleter], (tb, async) => { + tb.overrideView(MyComp, new View({ + template: '
', + directives: [bind(PublicApi).toClass(PrivateImpl), NeedsPublicApi] + })); + + tb.createView(MyComp, {context: ctx}).then((view) => { + async.done(); + }); + })); + it('should support template directives via `