feat(query): allow to query for TemplateRef

Part of #1989

Closes #3202
This commit is contained in:
Tobias Bosch
2015-07-22 14:24:57 -07:00
parent 5b5d31fa9a
commit 585ea5d600
5 changed files with 61 additions and 6 deletions
@@ -166,6 +166,12 @@ class NeedsQueryByVarBindings {
constructor(@Query("one,two") query: QueryList<any>) { this.query = query; }
}
@Injectable()
class NeedsTemplateRefQuery {
query: QueryList<TemplateRef>;
constructor(@Query(TemplateRef) query: QueryList<TemplateRef>) { this.query = query; }
}
@Injectable()
class NeedsElementRef {
elementRef;
@@ -1009,6 +1015,16 @@ export function main() {
expectDirectives(inj.get(NeedsQuery).query, CountingDirective, [0]);
})
it('should contain PreBuiltObjects on the same injector', () => {
var preBuiltObjects = new PreBuiltObjects(null, null, null, new TemplateRef(<any>new DummyElementRef()));
var inj = injector(ListWrapper.concat([
NeedsTemplateRefQuery
], extraBindings), null,
false, preBuiltObjects);
expect(inj.get(NeedsTemplateRefQuery).query.first).toBe(preBuiltObjects.templateRef);
});
it('should contain multiple directives from the same injector', () => {
var inj = injector(ListWrapper.concat([
NeedsQuery,
@@ -10,12 +10,13 @@ import {
it,
xit,
TestComponentBuilder,
asNativeElements
asNativeElements,
By
} from 'angular2/test_lib';
import {Injectable, Optional} from 'angular2/di';
import {QueryList} from 'angular2/core';
import {QueryList, TemplateRef} from 'angular2/core';
import {Query, ViewQuery, Component, Directive, View} from 'angular2/annotations';
import {NgIf, NgFor} from 'angular2/angular2';
@@ -126,6 +127,24 @@ export function main() {
}));
});
describe('query for TemplateRef', () => {
it('should find TemplateRefs in the light and shadow dom',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = '<needs-tpl><template var-x="light"></template></needs-tpl>';
tcb.overrideTemplate(MyComp, template)
.createAsync(MyComp)
.then((view) => {
view.detectChanges();
var needsTpl: NeedsTpl = view.componentViewChildren[0].inject(NeedsTpl);
expect(needsTpl.query.first.hasLocal('light')).toBe(true);
expect(needsTpl.viewQuery.first.hasLocal('shadow')).toBe(true);
async.done();
});
}));
});
describe("onChange", () => {
it('should notify query on change',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
@@ -545,6 +564,18 @@ class NeedsViewQueryOrder {
constructor(@ViewQuery(TextDirective) query: QueryList<TextDirective>) { this.query = query; }
}
@Component({selector: 'needs-tpl'})
@View({template: '<template var-x="shadow"></template>'})
class NeedsTpl {
viewQuery: QueryList<TemplateRef>;
query: QueryList<TemplateRef>;
constructor(@ViewQuery(TemplateRef) viewQuery: QueryList<TemplateRef>,
@Query(TemplateRef) query: QueryList<TemplateRef>) {
this.viewQuery = viewQuery;
this.query = query;
}
}
@Component({selector: 'my-comp'})
@View({
directives: [
@@ -558,6 +589,7 @@ class NeedsViewQueryOrder {
NeedsViewQueryIf,
NeedsViewQueryNestedIf,
NeedsViewQueryOrder,
NeedsTpl,
TextDirective,
InertDirective,
NgIf,