Skip to content

Commit 833d428

Browse files
crisbetommalerba
authored andcommitted
refactor: explicitly set static flag on all queries (#15680)
Explicitly marks all `ViewChild` and `ContentChild` queries so that the timing is consistent for apps using Ivy or ViewEngine. This PR resolves FW-1212.
1 parent 4816ecd commit 833d428

File tree

96 files changed

+348
-338
lines changed

Some content is hidden

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

96 files changed

+348
-338
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
@@ -3238,8 +3238,8 @@ describe('CdkDrag', () => {
32383238
`
32393239
})
32403240
class StandaloneDraggable {
3241-
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
3242-
@ViewChild(CdkDrag) dragInstance: CdkDrag;
3241+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
3242+
@ViewChild(CdkDrag, {static: false}) dragInstance: CdkDrag;
32433243
startedSpy = jasmine.createSpy('started spy');
32443244
endedSpy = jasmine.createSpy('ended spy');
32453245
releasedSpy = jasmine.createSpy('released spy');
@@ -3256,8 +3256,8 @@ class StandaloneDraggable {
32563256
`
32573257
})
32583258
class StandaloneDraggableWithOnPush {
3259-
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
3260-
@ViewChild(CdkDrag) dragInstance: CdkDrag;
3259+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
3260+
@ViewChild(CdkDrag, {static: false}) dragInstance: CdkDrag;
32613261
}
32623262

32633263
@Component({
@@ -3270,7 +3270,7 @@ class StandaloneDraggableWithOnPush {
32703270
`
32713271
})
32723272
class StandaloneDraggableSvg {
3273-
@ViewChild('dragElement') dragElement: ElementRef<SVGElement>;
3273+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<SVGElement>;
32743274
}
32753275

32763276
@Component({
@@ -3282,10 +3282,10 @@ class StandaloneDraggableSvg {
32823282
`
32833283
})
32843284
class StandaloneDraggableWithHandle {
3285-
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
3286-
@ViewChild('handleElement') handleElement: ElementRef<HTMLElement>;
3287-
@ViewChild(CdkDrag) dragInstance: CdkDrag;
3288-
@ViewChild(CdkDragHandle) handleInstance: CdkDragHandle;
3285+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
3286+
@ViewChild('handleElement', {static: false}) handleElement: ElementRef<HTMLElement>;
3287+
@ViewChild(CdkDrag, {static: false}) dragInstance: CdkDrag;
3288+
@ViewChild(CdkDragHandle, {static: false}) handleInstance: CdkDragHandle;
32893289
}
32903290

32913291
@Component({
@@ -3300,8 +3300,8 @@ class StandaloneDraggableWithHandle {
33003300
`
33013301
})
33023302
class StandaloneDraggableWithDelayedHandle {
3303-
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
3304-
@ViewChild('handleElement') handleElement: ElementRef<HTMLElement>;
3303+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
3304+
@ViewChild('handleElement', {static: false}) handleElement: ElementRef<HTMLElement>;
33053305
showHandle = false;
33063306
}
33073307

@@ -3320,8 +3320,8 @@ class StandaloneDraggableWithDelayedHandle {
33203320
`
33213321
})
33223322
class StandaloneDraggableWithIndirectHandle {
3323-
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
3324-
@ViewChild('handleElement') handleElement: ElementRef<HTMLElement>;
3323+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
3324+
@ViewChild('handleElement', {static: false}) handleElement: ElementRef<HTMLElement>;
33253325
}
33263326

33273327

@@ -3345,7 +3345,7 @@ class StandaloneDraggableWithIndirectHandle {
33453345
`
33463346
})
33473347
class StandaloneDraggableWithMultipleHandles {
3348-
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
3348+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
33493349
@ViewChildren(CdkDragHandle) handles: QueryList<CdkDragHandle>;
33503350
}
33513351

@@ -3371,7 +3371,7 @@ const DROP_ZONE_FIXTURE_TEMPLATE = `
33713371
@Component({template: DROP_ZONE_FIXTURE_TEMPLATE})
33723372
class DraggableInDropZone {
33733373
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
3374-
@ViewChild(CdkDropList) dropInstance: CdkDropList;
3374+
@ViewChild(CdkDropList, {static: false}) dropInstance: CdkDropList;
33753375
items = [
33763376
{value: 'Zero', height: ITEM_HEIGHT, margin: 0},
33773377
{value: 'One', height: ITEM_HEIGHT, margin: 0},
@@ -3427,7 +3427,7 @@ class DraggableInOnPushDropZone extends DraggableInDropZone {}
34273427
})
34283428
class DraggableInHorizontalDropZone {
34293429
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
3430-
@ViewChild(CdkDropList) dropInstance: CdkDropList;
3430+
@ViewChild(CdkDropList, {static: false}) dropInstance: CdkDropList;
34313431
items = [
34323432
{value: 'Zero', width: ITEM_WIDTH, margin: 0},
34333433
{value: 'One', width: ITEM_WIDTH, margin: 0},
@@ -3461,7 +3461,7 @@ class DraggableInHorizontalDropZone {
34613461
`
34623462
})
34633463
class DraggableInDropZoneWithCustomPreview {
3464-
@ViewChild(CdkDropList) dropInstance: CdkDropList;
3464+
@ViewChild(CdkDropList, {static: false}) dropInstance: CdkDropList;
34653465
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
34663466
items = ['Zero', 'One', 'Two', 'Three'];
34673467
boundarySelector: string;
@@ -3606,9 +3606,9 @@ class ConnectedDropZonesViaGroupDirective extends ConnectedDropZones {
36063606
`
36073607
})
36083608
class DraggableWithAlternateRoot {
3609-
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
3610-
@ViewChild('dragRoot') dragRoot: ElementRef<HTMLElement>;
3611-
@ViewChild(CdkDrag) dragInstance: CdkDrag;
3609+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
3610+
@ViewChild('dragRoot', {static: false}) dragRoot: ElementRef<HTMLElement>;
3611+
@ViewChild(CdkDrag, {static: false}) dragInstance: CdkDrag;
36123612
rootElementSelector: string;
36133613
}
36143614

@@ -3667,9 +3667,9 @@ class ConnectedDropZonesWithSingleItems {
36673667
`
36683668
})
36693669
class NestedDropListGroups {
3670-
@ViewChild('group') group: CdkDropListGroup<CdkDropList>;
3671-
@ViewChild('listOne') listOne: CdkDropList;
3672-
@ViewChild('listTwo') listTwo: CdkDropList;
3670+
@ViewChild('group', {static: false}) group: CdkDropListGroup<CdkDropList>;
3671+
@ViewChild('listOne', {static: false}) listOne: CdkDropList;
3672+
@ViewChild('listTwo', {static: false}) listTwo: CdkDropList;
36733673
}
36743674

36753675

@@ -3694,7 +3694,7 @@ class DraggableOnNgContainer {}
36943694
})
36953695
class DraggableInDropZoneWithoutEvents {
36963696
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
3697-
@ViewChild(CdkDropList) dropInstance: CdkDropList;
3697+
@ViewChild(CdkDropList, {static: false}) dropInstance: CdkDropList;
36983698
items = [
36993699
{value: 'Zero', height: ITEM_HEIGHT},
37003700
{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
@@ -82,10 +82,10 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
8282
@ContentChildren(CdkDragHandle, {descendants: true}) _handles: QueryList<CdkDragHandle>;
8383

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

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

9090
/** Arbitrary data to attach to this drag instance. */
9191
@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)