X Tutup
Skip to content

Commit 34acef5

Browse files
committed
fix(query): view query should not be updated when subviews are attached.
1 parent c1ee943 commit 34acef5

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

modules/angular2/src/core/compiler/element_injector.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,10 @@ export class ElementInjector extends TreeNode<ElementInjector> implements Depend
789789
}
790790

791791
this.remove();
792-
793-
ListWrapper.forEach(queriesToUpdate, (q) => q.update());
792+
// TODO(rado): update should work on view queries too, however currently it
793+
// is not implemented, so we filter to non-view queries.
794+
var nonViewQueries = ListWrapper.filter(queriesToUpdate, (q) => !q.query.isViewQuery);
795+
ListWrapper.forEach(nonViewQueries, (q) => q.update());
794796
}
795797

796798
private _pruneQueryFromTree(query: QueryRef): void {

modules/angular2/test/core/compiler/query_integration_spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,30 @@ export function main() {
365365
});
366366
}));
367367

368+
it('should not be affected by other changes in the component',
369+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
370+
var template = '<needs-view-query-nested-if #q></needs-view-query-nested-if>';
371+
372+
tcb.overrideTemplate(MyComp, template)
373+
.createAsync(MyComp)
374+
.then((view) => {
375+
var q: NeedsViewQueryNestedIf = view.componentViewChildren[0].getLocal("q");
376+
377+
view.detectChanges();
378+
379+
expect(q.query.length).toEqual(1);
380+
expect(q.query.first.text).toEqual("1");
381+
382+
q.show = false;
383+
view.detectChanges();
384+
385+
expect(q.query.length).toEqual(1);
386+
expect(q.query.first.text).toEqual("1");
387+
388+
async.done();
389+
});
390+
}));
391+
368392
/* TODO(rado): fix and reenable.
369393
370394
it('should maintain directives in pre-order depth-first DOM order after dynamic insertion',
@@ -396,6 +420,12 @@ class TextDirective {
396420
constructor() {}
397421
}
398422

423+
@Directive({selector: '[dir]'})
424+
@Injectable()
425+
class InertDirective {
426+
constructor() {}
427+
}
428+
399429
@Component({selector: 'needs-query'})
400430
@View({
401431
directives: [NgFor, TextDirective],
@@ -487,6 +517,22 @@ class NeedsViewQueryIf {
487517
}
488518

489519

520+
@Component({selector: 'needs-view-query-nested-if'})
521+
@View({
522+
directives: [NgIf, InertDirective, TextDirective],
523+
template: '<div text="1"><div *ng-if="show"><div dir></div></div></div>'
524+
})
525+
@Injectable()
526+
class NeedsViewQueryNestedIf {
527+
show: boolean;
528+
query: QueryList<TextDirective>;
529+
constructor(@ViewQuery(TextDirective) query: QueryList<TextDirective>) {
530+
this.query = query;
531+
this.show = true;
532+
}
533+
}
534+
535+
490536
@Component({selector: 'needs-view-query-order'})
491537
@View({
492538
directives: [NgFor, TextDirective],
@@ -511,8 +557,10 @@ class NeedsViewQueryOrder {
511557
NeedsViewQuery,
512558
NeedsViewQueryDesc,
513559
NeedsViewQueryIf,
560+
NeedsViewQueryNestedIf,
514561
NeedsViewQueryOrder,
515562
TextDirective,
563+
InertDirective,
516564
NgIf,
517565
NgFor
518566
]

0 commit comments

Comments
 (0)
X Tutup