Skip to content

Commit bb08489

Browse files
committed
refactor: explicitly set static flag on all queries
Explicitly marks all `ViewChild` and `ContentChild` queries so that the timing is consistent for apps using Ivy or ViewEngine.
1 parent 4b93a6e commit bb08489

File tree

98 files changed

+358
-348
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+358
-348
lines changed

src/a11y-demo/a11y-demo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export class AccessibilityDemoRoot implements OnDestroy {
2121

2222
private _routerSubscription = Subscription.EMPTY;
2323

24-
@ViewChild('maincontent') mainContent: ElementRef<HTMLElement>;
25-
@ViewChild('header') sectionHeader: ElementRef<HTMLElement>;
24+
@ViewChild('maincontent', {static: false}) mainContent: ElementRef<HTMLElement>;
25+
@ViewChild('header', {static: false}) sectionHeader: ElementRef<HTMLElement>;
2626

2727
navItems = [
2828
{name: 'Home', route: '.'},

src/a11y-demo/table/table-a11y.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const exampleData = [
3535
styleUrls: ['table-a11y.css'],
3636
})
3737
export class TableAccessibilityDemo implements OnInit {
38-
@ViewChild(MatSort) sort: MatSort;
39-
@ViewChild(MatPaginator) pager: MatPaginator;
38+
@ViewChild(MatSort, {static: true}) sort: MatSort;
39+
@ViewChild(MatPaginator, {static: true}) pager: MatPaginator;
4040

4141
displayedColumns = ['name', 'color', 'age'];
4242
basicDataSource: BasicDataSource;

src/cdk-experimental/dialog/dialog.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,9 @@ class ComponentWithOnPushViewContainer {
10791079
template: `<dir-with-view-container></dir-with-view-container>`,
10801080
})
10811081
class ComponentWithChildViewContainer {
1082-
@ViewChild(DirectiveWithViewContainer) childWithViewContainer: DirectiveWithViewContainer;
1082+
@ViewChild(DirectiveWithViewContainer, {
1083+
static: false
1084+
}) childWithViewContainer: DirectiveWithViewContainer;
10831085

10841086
get childViewContainer() {
10851087
return this.childWithViewContainer.viewContainerRef;
@@ -1095,7 +1097,7 @@ class ComponentWithTemplateRef {
10951097
localValue: string;
10961098
dialogRef: DialogRef<any>;
10971099

1098-
@ViewChild(TemplateRef) templateRef: TemplateRef<any>;
1100+
@ViewChild(TemplateRef, {static: false}) templateRef: TemplateRef<any>;
10991101

11001102
setDialogRef(dialogRef: DialogRef<any>): string {
11011103
this.dialogRef = dialogRef;

src/cdk-experimental/scrolling/virtual-scroll-viewport.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function finishInit(fixture: ComponentFixture<any>) {
9191
encapsulation: ViewEncapsulation.None,
9292
})
9393
class AutoSizeVirtualScroll {
94-
@ViewChild(CdkVirtualScrollViewport) viewport: CdkVirtualScrollViewport;
94+
@ViewChild(CdkVirtualScrollViewport, {static: true}) viewport: CdkVirtualScrollViewport;
9595

9696
@Input() orientation = 'vertical';
9797
@Input() viewportSize = 200;

src/cdk/a11y/aria-describer/aria-describer.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,16 @@ function expectMessage(el: Element, message: string) {
189189
`,
190190
})
191191
class TestApp {
192-
@ViewChild('element1') _element1: ElementRef<HTMLElement>;
192+
@ViewChild('element1', {static: false}) _element1: ElementRef<HTMLElement>;
193193
get element1(): Element { return this._element1.nativeElement; }
194194

195-
@ViewChild('element2') _element2: ElementRef<HTMLElement>;
195+
@ViewChild('element2', {static: false}) _element2: ElementRef<HTMLElement>;
196196
get element2(): Element { return this._element2.nativeElement; }
197197

198-
@ViewChild('element3') _element3: ElementRef<HTMLElement>;
198+
@ViewChild('element3', {static: false}) _element3: ElementRef<HTMLElement>;
199199
get element3(): Element { return this._element3.nativeElement; }
200200

201-
@ViewChild('element4') _element4: ElementRef<HTMLElement>;
201+
@ViewChild('element4', {static: false}) _element4: ElementRef<HTMLElement>;
202202
get element4(): Element { return this._element4.nativeElement; }
203203

204204

src/cdk/a11y/focus-trap/focus-trap.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('FocusTrap', () => {
199199
`
200200
})
201201
class SimpleFocusTrap {
202-
@ViewChild(CdkTrapFocus) focusTrapDirective: CdkTrapFocus;
202+
@ViewChild(CdkTrapFocus, {static: false}) focusTrapDirective: CdkTrapFocus;
203203
}
204204

205205
@Component({
@@ -212,7 +212,7 @@ class SimpleFocusTrap {
212212
`
213213
})
214214
class FocusTrapWithAutoCapture {
215-
@ViewChild(CdkTrapFocus) focusTrapDirective: CdkTrapFocus;
215+
@ViewChild(CdkTrapFocus, {static: false}) focusTrapDirective: CdkTrapFocus;
216216
showTrappedRegion = false;
217217
}
218218

@@ -226,7 +226,7 @@ class FocusTrapWithAutoCapture {
226226
`
227227
})
228228
class FocusTrapWithBindings {
229-
@ViewChild(CdkTrapFocus) focusTrapDirective: CdkTrapFocus;
229+
@ViewChild(CdkTrapFocus, {static: false}) focusTrapDirective: CdkTrapFocus;
230230
renderFocusTrap = true;
231231
_isFocusTrapEnabled = true;
232232
}
@@ -246,7 +246,7 @@ class FocusTrapWithBindings {
246246
`
247247
})
248248
class FocusTrapTargets {
249-
@ViewChild(CdkTrapFocus) focusTrapDirective: CdkTrapFocus;
249+
@ViewChild(CdkTrapFocus, {static: false}) focusTrapDirective: CdkTrapFocus;
250250
}
251251

252252
@Component({
@@ -257,7 +257,7 @@ class FocusTrapTargets {
257257
`
258258
})
259259
class FocusTrapUnfocusableTarget {
260-
@ViewChild(CdkTrapFocus) focusTrapDirective: CdkTrapFocus;
260+
@ViewChild(CdkTrapFocus, {static: false}) focusTrapDirective: CdkTrapFocus;
261261
}
262262

263263
@Component({
@@ -270,7 +270,7 @@ class FocusTrapUnfocusableTarget {
270270
`
271271
})
272272
class FocusTrapWithSvg {
273-
@ViewChild(CdkTrapFocus) focusTrapDirective: CdkTrapFocus;
273+
@ViewChild(CdkTrapFocus, {static: false}) focusTrapDirective: CdkTrapFocus;
274274
}
275275

276276
@Component({
@@ -281,5 +281,5 @@ class FocusTrapWithSvg {
281281
`
282282
})
283283
class FocusTrapWithoutFocusableElements {
284-
@ViewChild(CdkTrapFocus) focusTrapDirective: CdkTrapFocus;
284+
@ViewChild(CdkTrapFocus, {static: false}) focusTrapDirective: CdkTrapFocus;
285285
}

src/cdk/bidi/directionality.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ describe('Directionality', () => {
158158
`
159159
})
160160
class ElementWithDir {
161-
@ViewChild(Dir) dir: Dir;
161+
@ViewChild(Dir, {static: false}) dir: Dir;
162162
direction = 'rtl';
163163
changeCount = 0;
164164
}
@@ -167,14 +167,14 @@ class ElementWithDir {
167167
template: '<div dir="auto"></div>'
168168
})
169169
class ElementWithPredefinedAutoDir {
170-
@ViewChild(Dir) dir: Dir;
170+
@ViewChild(Dir, {static: false}) dir: Dir;
171171
}
172172

173173
@Component({
174174
template: '<div dir="RTL"></div>'
175175
})
176176
class ElementWithPredefinedUppercaseDir {
177-
@ViewChild(Dir) dir: Dir;
177+
@ViewChild(Dir, {static: false}) dir: Dir;
178178
}
179179

180180

src/cdk/drag-drop/directives/drag.spec.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3196,8 +3196,8 @@ describe('CdkDrag', () => {
31963196
`
31973197
})
31983198
class StandaloneDraggable {
3199-
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
3200-
@ViewChild(CdkDrag) dragInstance: CdkDrag;
3199+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
3200+
@ViewChild(CdkDrag, {static: false}) dragInstance: CdkDrag;
32013201
startedSpy = jasmine.createSpy('started spy');
32023202
endedSpy = jasmine.createSpy('ended spy');
32033203
releasedSpy = jasmine.createSpy('released spy');
@@ -3213,8 +3213,8 @@ class StandaloneDraggable {
32133213
`
32143214
})
32153215
class StandaloneDraggableWithOnPush {
3216-
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
3217-
@ViewChild(CdkDrag) dragInstance: CdkDrag;
3216+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
3217+
@ViewChild(CdkDrag, {static: false}) dragInstance: CdkDrag;
32183218
}
32193219

32203220
@Component({
@@ -3227,7 +3227,7 @@ class StandaloneDraggableWithOnPush {
32273227
`
32283228
})
32293229
class StandaloneDraggableSvg {
3230-
@ViewChild('dragElement') dragElement: ElementRef<SVGElement>;
3230+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<SVGElement>;
32313231
}
32323232

32333233
@Component({
@@ -3239,10 +3239,10 @@ class StandaloneDraggableSvg {
32393239
`
32403240
})
32413241
class StandaloneDraggableWithHandle {
3242-
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
3243-
@ViewChild('handleElement') handleElement: ElementRef<HTMLElement>;
3244-
@ViewChild(CdkDrag) dragInstance: CdkDrag;
3245-
@ViewChild(CdkDragHandle) handleInstance: CdkDragHandle;
3242+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
3243+
@ViewChild('handleElement', {static: false}) handleElement: ElementRef<HTMLElement>;
3244+
@ViewChild(CdkDrag, {static: false}) dragInstance: CdkDrag;
3245+
@ViewChild(CdkDragHandle, {static: false}) handleInstance: CdkDragHandle;
32463246
}
32473247

32483248
@Component({
@@ -3257,8 +3257,8 @@ class StandaloneDraggableWithHandle {
32573257
`
32583258
})
32593259
class StandaloneDraggableWithDelayedHandle {
3260-
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
3261-
@ViewChild('handleElement') handleElement: ElementRef<HTMLElement>;
3260+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
3261+
@ViewChild('handleElement', {static: false}) handleElement: ElementRef<HTMLElement>;
32623262
showHandle = false;
32633263
}
32643264

@@ -3277,8 +3277,8 @@ class StandaloneDraggableWithDelayedHandle {
32773277
`
32783278
})
32793279
class StandaloneDraggableWithIndirectHandle {
3280-
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
3281-
@ViewChild('handleElement') handleElement: ElementRef<HTMLElement>;
3280+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
3281+
@ViewChild('handleElement', {static: false}) handleElement: ElementRef<HTMLElement>;
32823282
}
32833283

32843284

@@ -3302,7 +3302,7 @@ class StandaloneDraggableWithIndirectHandle {
33023302
`
33033303
})
33043304
class StandaloneDraggableWithMultipleHandles {
3305-
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
3305+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
33063306
@ViewChildren(CdkDragHandle) handles: QueryList<CdkDragHandle>;
33073307
}
33083308

@@ -3328,7 +3328,7 @@ const DROP_ZONE_FIXTURE_TEMPLATE = `
33283328
@Component({template: DROP_ZONE_FIXTURE_TEMPLATE})
33293329
class DraggableInDropZone {
33303330
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
3331-
@ViewChild(CdkDropList) dropInstance: CdkDropList;
3331+
@ViewChild(CdkDropList, {static: false}) dropInstance: CdkDropList;
33323332
items = [
33333333
{value: 'Zero', height: ITEM_HEIGHT, margin: 0},
33343334
{value: 'One', height: ITEM_HEIGHT, margin: 0},
@@ -3384,7 +3384,7 @@ class DraggableInOnPushDropZone extends DraggableInDropZone {}
33843384
})
33853385
class DraggableInHorizontalDropZone {
33863386
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
3387-
@ViewChild(CdkDropList) dropInstance: CdkDropList;
3387+
@ViewChild(CdkDropList, {static: false}) dropInstance: CdkDropList;
33883388
items = [
33893389
{value: 'Zero', width: ITEM_WIDTH, margin: 0},
33903390
{value: 'One', width: ITEM_WIDTH, margin: 0},
@@ -3418,7 +3418,7 @@ class DraggableInHorizontalDropZone {
34183418
`
34193419
})
34203420
class DraggableInDropZoneWithCustomPreview {
3421-
@ViewChild(CdkDropList) dropInstance: CdkDropList;
3421+
@ViewChild(CdkDropList, {static: false}) dropInstance: CdkDropList;
34223422
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
34233423
items = ['Zero', 'One', 'Two', 'Three'];
34243424
boundarySelector: string;
@@ -3563,9 +3563,9 @@ class ConnectedDropZonesViaGroupDirective extends ConnectedDropZones {
35633563
`
35643564
})
35653565
class DraggableWithAlternateRoot {
3566-
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
3567-
@ViewChild('dragRoot') dragRoot: ElementRef<HTMLElement>;
3568-
@ViewChild(CdkDrag) dragInstance: CdkDrag;
3566+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
3567+
@ViewChild('dragRoot', {static: false}) dragRoot: ElementRef<HTMLElement>;
3568+
@ViewChild(CdkDrag, {static: false}) dragInstance: CdkDrag;
35693569
rootElementSelector: string;
35703570
}
35713571

@@ -3624,9 +3624,9 @@ class ConnectedDropZonesWithSingleItems {
36243624
`
36253625
})
36263626
class NestedDropListGroups {
3627-
@ViewChild('group') group: CdkDropListGroup<CdkDropList>;
3628-
@ViewChild('listOne') listOne: CdkDropList;
3629-
@ViewChild('listTwo') listTwo: CdkDropList;
3627+
@ViewChild('group', {static: false}) group: CdkDropListGroup<CdkDropList>;
3628+
@ViewChild('listOne', {static: false}) listOne: CdkDropList;
3629+
@ViewChild('listTwo', {static: false}) listTwo: CdkDropList;
36303630
}
36313631

36323632

@@ -3651,7 +3651,7 @@ class DraggableOnNgContainer {}
36513651
})
36523652
class DraggableInDropZoneWithoutEvents {
36533653
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
3654-
@ViewChild(CdkDropList) dropInstance: CdkDropList;
3654+
@ViewChild(CdkDropList, {static: false}) dropInstance: CdkDropList;
36553655
items = [
36563656
{value: 'Zero', height: ITEM_HEIGHT},
36573657
{value: 'One', height: ITEM_HEIGHT},

src/cdk/drag-drop/directives/drag.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
8585
@ContentChildren(CdkDragHandle, {descendants: true}) _handles: QueryList<CdkDragHandle>;
8686

8787
/** Element that will be used as a template to create the draggable item's preview. */
88-
@ContentChild(CdkDragPreview) _previewTemplate: CdkDragPreview;
88+
@ContentChild(CdkDragPreview, {static: false}) _previewTemplate: CdkDragPreview;
8989

9090
/** Template for placeholder element rendered to show where a draggable would be dropped. */
91-
@ContentChild(CdkDragPlaceholder) _placeholderTemplate: CdkDragPlaceholder;
91+
@ContentChild(CdkDragPlaceholder, {static: false}) _placeholderTemplate: CdkDragPlaceholder;
9292

9393
/** Arbitrary data to attach to this drag instance. */
9494
@Input('cdkDragData') data: T;

src/cdk/observers/observe-content.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,6 @@ class ComponentWithDebouncedListener {
228228
template: `<div #contentEl>{{text}}</div>`
229229
})
230230
class UnobservedComponentWithTextContent {
231-
@ViewChild('contentEl') contentEl: ElementRef;
231+
@ViewChild('contentEl', {static: false}) contentEl: ElementRef;
232232
text = '';
233233
}

src/cdk/overlay/overlay-directives.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,9 @@ describe('Overlay directives', () => {
539539
</ng-template>`,
540540
})
541541
class ConnectedOverlayDirectiveTest {
542-
@ViewChild(CdkConnectedOverlay) connectedOverlayDirective: CdkConnectedOverlay;
543-
@ViewChild('trigger') trigger: CdkOverlayOrigin;
544-
@ViewChild('otherTrigger') otherTrigger: CdkOverlayOrigin;
542+
@ViewChild(CdkConnectedOverlay, {static: false}) connectedOverlayDirective: CdkConnectedOverlay;
543+
@ViewChild('trigger', {static: false}) trigger: CdkOverlayOrigin;
544+
@ViewChild('otherTrigger', {static: false}) otherTrigger: CdkOverlayOrigin;
545545

546546
isOpen = false;
547547
width: number | string;
@@ -574,6 +574,6 @@ class ConnectedOverlayDirectiveTest {
574574
<ng-template cdk-connected-overlay>Menu content</ng-template>`,
575575
})
576576
class ConnectedOverlayPropertyInitOrder {
577-
@ViewChild(CdkConnectedOverlay) connectedOverlayDirective: CdkConnectedOverlay;
578-
@ViewChild('trigger') trigger: CdkOverlayOrigin;
577+
@ViewChild(CdkConnectedOverlay, {static: false}) connectedOverlayDirective: CdkConnectedOverlay;
578+
@ViewChild('trigger', {static: false}) trigger: CdkOverlayOrigin;
579579
}

src/cdk/overlay/overlay.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ class PizzaMsg { }
948948
/** Test-bed component that contains a TempatePortal and an ElementRef. */
949949
@Component({template: `<ng-template cdk-portal>Cake</ng-template>`})
950950
class TestComponentWithTemplatePortals {
951-
@ViewChild(CdkPortal) templatePortal: CdkPortal;
951+
@ViewChild(CdkPortal, {static: false}) templatePortal: CdkPortal;
952952

953953
constructor(public viewContainerRef: ViewContainerRef) { }
954954
}

src/cdk/scrolling/scroll-dispatcher.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ describe('ScrollDispatcher', () => {
244244
template: `<div #scrollingElement cdk-scrollable style="height: 9999px"></div>`
245245
})
246246
class ScrollingComponent {
247-
@ViewChild(CdkScrollable) scrollable: CdkScrollable;
248-
@ViewChild('scrollingElement') scrollingElement: ElementRef<HTMLElement>;
247+
@ViewChild(CdkScrollable, {static: false}) scrollable: CdkScrollable;
248+
@ViewChild('scrollingElement', {static: false}) scrollingElement: ElementRef<HTMLElement>;
249249
}
250250

251251

@@ -262,7 +262,7 @@ class ScrollingComponent {
262262
`
263263
})
264264
class NestedScrollingComponent {
265-
@ViewChild('interestingElement') interestingElement: ElementRef<HTMLElement>;
265+
@ViewChild('interestingElement', {static: false}) interestingElement: ElementRef<HTMLElement>;
266266
}
267267

268268
const TEST_COMPONENTS = [ScrollingComponent, NestedScrollingComponent];

src/cdk/scrolling/scrollable.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ describe('CdkScrollable', () => {
241241
})
242242
class ScrollableViewport {
243243
@Input() dir: Direction;
244-
@ViewChild(CdkScrollable) scrollable: CdkScrollable;
245-
@ViewChild('scrollContainer') scrollContainer: ElementRef<Element>;
246-
@ViewChild('firstRowStart') firstRowStart: ElementRef<Element>;
247-
@ViewChild('firstRowEnd') firstRowEnd: ElementRef<Element>;
248-
@ViewChild('lastRowStart') lastRowStart: ElementRef<Element>;
249-
@ViewChild('lastRowEnd') lastRowEnd: ElementRef<Element>;
244+
@ViewChild(CdkScrollable, {static: false}) scrollable: CdkScrollable;
245+
@ViewChild('scrollContainer', {static: false}) scrollContainer: ElementRef<Element>;
246+
@ViewChild('firstRowStart', {static: false}) firstRowStart: ElementRef<Element>;
247+
@ViewChild('firstRowEnd', {static: false}) firstRowEnd: ElementRef<Element>;
248+
@ViewChild('lastRowStart', {static: false}) lastRowStart: ElementRef<Element>;
249+
@ViewChild('lastRowEnd', {static: false}) lastRowEnd: ElementRef<Element>;
250250
}

src/cdk/stepper/stepper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class CdkStep implements OnChanges {
118118
_displayDefaultIndicatorType: boolean;
119119

120120
/** Template for step label if it exists. */
121-
@ContentChild(CdkStepLabel) stepLabel: CdkStepLabel;
121+
@ContentChild(CdkStepLabel, {static: false}) stepLabel: CdkStepLabel;
122122

123123
/** Template for step content. */
124124
@ViewChild(TemplateRef, {static: true}) content: TemplateRef<any>;

0 commit comments

Comments
 (0)