Skip to content

Commit 4bab25b

Browse files
pkozlowski-opensourcetbosch
authored andcommitted
feat: alllow specifying directives as bindings
Related to angular#709 Closes angular#1498
1 parent 6896305 commit 4bab25b

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

modules/angular2/src/core/compiler/compiler.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Injectable} from 'angular2/di';
1+
import {Injectable, Binding} from 'angular2/di';
22
import {Type, isBlank, isPresent, BaseException, normalizeBlank, stringify} from 'angular2/src/facade/lang';
33
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
44
import {List, ListWrapper, Map, MapWrapper} from 'angular2/src/facade/collection';
@@ -76,9 +76,13 @@ export class Compiler {
7676
_bindDirective(directiveTypeOrBinding):DirectiveBinding {
7777
if (directiveTypeOrBinding instanceof DirectiveBinding) {
7878
return directiveTypeOrBinding;
79+
} else if (directiveTypeOrBinding instanceof Binding) {
80+
let meta = this._reader.read(directiveTypeOrBinding.token);
81+
return DirectiveBinding.createFromBinding(directiveTypeOrBinding, meta.annotation);
82+
} else {
83+
let meta = this._reader.read(directiveTypeOrBinding);
84+
return DirectiveBinding.createFromType(meta.type, meta.annotation);
7985
}
80-
var meta = this._reader.read(directiveTypeOrBinding);
81-
return DirectiveBinding.createFromType(meta.type, meta.annotation);
8286
}
8387

8488
// Create a hostView as if the compiler encountered <hostcmp></hostcmp>.

modules/angular2/test/core/compiler/integration_spec.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,31 @@ export function main() {
274274
});
275275
}));
276276

277+
it('should allow specifying directives as bindings', inject([TestBed, AsyncTestCompleter], (tb, async) => {
278+
tb.overrideView(MyComp, new View({
279+
template: '<child-cmp></child-cmp>',
280+
directives: [bind(ChildComp).toClass(ChildComp)]
281+
}));
282+
283+
tb.createView(MyComp, {context: ctx}).then((view) => {
284+
view.detectChanges();
285+
286+
expect(view.rootNodes).toHaveText('hello');
287+
async.done();
288+
});
289+
}));
290+
291+
it('should read directives metadata from their binding token', inject([TestBed, AsyncTestCompleter], (tb, async) => {
292+
tb.overrideView(MyComp, new View({
293+
template: '<div public-api><div needs-public-api></div></div>',
294+
directives: [bind(PublicApi).toClass(PrivateImpl), NeedsPublicApi]
295+
}));
296+
297+
tb.createView(MyComp, {context: ctx}).then((view) => {
298+
async.done();
299+
});
300+
}));
301+
277302
it('should support template directives via `<template>` elements.', inject([TestBed, AsyncTestCompleter], (tb, async) => {
278303
tb.overrideView(MyComp,
279304
new View({
@@ -1191,3 +1216,24 @@ class NeedsAttribute {
11911216
this.fooAttribute = fooAttribute;
11921217
}
11931218
}
1219+
1220+
@Decorator({
1221+
selector: '[public-api]'
1222+
})
1223+
class PublicApi {
1224+
}
1225+
1226+
@Decorator({
1227+
selector: '[private-impl]'
1228+
})
1229+
class PrivateImpl extends PublicApi {
1230+
}
1231+
1232+
@Decorator({
1233+
selector: '[needs-public-api]'
1234+
})
1235+
class NeedsPublicApi {
1236+
constructor(@Parent() api:PublicApi) {
1237+
expect(api instanceof PrivateImpl).toBe(true);
1238+
}
1239+
}

0 commit comments

Comments
 (0)