Skip to content

Commit 469f8e2

Browse files
authored
docs: reduce reliance on static queries in live examples (#20452)
Removes (almost) all of the static queries from the live examples since it's something people shouldn't be depending on. There's one place left which can't be removed without changes in the table code.
1 parent e01fac4 commit 469f8e2

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

src/components-examples/cdk/portal/cdk-portal-overview/cdk-portal-overview-example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
})
1919
export class CdkPortalOverviewExample implements AfterViewInit {
2020
@ViewChild('templatePortalContent') templatePortalContent: TemplateRef<unknown>;
21-
@ViewChild('domPortalContent', {static: true}) domPortalContent: ElementRef<HTMLElement>;
21+
@ViewChild('domPortalContent') domPortalContent: ElementRef<HTMLElement>;
2222

2323
selectedPortal: Portal<any>;
2424
componentPortal: ComponentPortal<ComponentPortalExample>;

src/components-examples/material/table/table-overview/table-overview-example.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, OnInit, ViewChild} from '@angular/core';
1+
import {AfterViewInit, Component, ViewChild} from '@angular/core';
22
import {MatPaginator} from '@angular/material/paginator';
33
import {MatSort} from '@angular/material/sort';
44
import {MatTableDataSource} from '@angular/material/table';
@@ -28,12 +28,12 @@ const NAMES: string[] = [
2828
styleUrls: ['table-overview-example.css'],
2929
templateUrl: 'table-overview-example.html',
3030
})
31-
export class TableOverviewExample implements OnInit {
31+
export class TableOverviewExample implements AfterViewInit {
3232
displayedColumns: string[] = ['id', 'name', 'progress', 'color'];
3333
dataSource: MatTableDataSource<UserData>;
3434

35-
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
36-
@ViewChild(MatSort, {static: true}) sort: MatSort;
35+
@ViewChild(MatPaginator) paginator: MatPaginator;
36+
@ViewChild(MatSort) sort: MatSort;
3737

3838
constructor() {
3939
// Create 100 users
@@ -43,7 +43,7 @@ export class TableOverviewExample implements OnInit {
4343
this.dataSource = new MatTableDataSource(users);
4444
}
4545

46-
ngOnInit() {
46+
ngAfterViewInit() {
4747
this.dataSource.paginator = this.paginator;
4848
this.dataSource.sort = this.sort;
4949
}

src/components-examples/material/table/table-pagination/table-pagination-example.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, OnInit, ViewChild} from '@angular/core';
1+
import {AfterViewInit, Component, ViewChild} from '@angular/core';
22
import {MatPaginator} from '@angular/material/paginator';
33
import {MatTableDataSource} from '@angular/material/table';
44

@@ -10,13 +10,13 @@ import {MatTableDataSource} from '@angular/material/table';
1010
styleUrls: ['table-pagination-example.css'],
1111
templateUrl: 'table-pagination-example.html',
1212
})
13-
export class TablePaginationExample implements OnInit {
13+
export class TablePaginationExample implements AfterViewInit {
1414
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
1515
dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
1616

17-
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
17+
@ViewChild(MatPaginator) paginator: MatPaginator;
1818

19-
ngOnInit() {
19+
ngAfterViewInit() {
2020
this.dataSource.paginator = this.paginator;
2121
}
2222
}

src/components-examples/material/table/table-sorting/table-sorting-example.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, OnInit, ViewChild} from '@angular/core';
1+
import {AfterViewInit, Component, ViewChild} from '@angular/core';
22
import {MatSort} from '@angular/material/sort';
33
import {MatTableDataSource} from '@angular/material/table';
44

@@ -30,13 +30,13 @@ const ELEMENT_DATA: PeriodicElement[] = [
3030
styleUrls: ['table-sorting-example.css'],
3131
templateUrl: 'table-sorting-example.html',
3232
})
33-
export class TableSortingExample implements OnInit {
33+
export class TableSortingExample implements AfterViewInit {
3434
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
3535
dataSource = new MatTableDataSource(ELEMENT_DATA);
3636

37-
@ViewChild(MatSort, {static: true}) sort: MatSort;
37+
@ViewChild(MatSort) sort: MatSort;
3838

39-
ngOnInit() {
39+
ngAfterViewInit() {
4040
this.dataSource.sort = this.sort;
4141
}
4242
}

src/components-examples/material/table/table-wrapped/table-wrapped-example.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
Component,
55
ContentChildren,
66
Input,
7-
OnInit,
7+
AfterViewInit,
88
QueryList,
99
ViewChild
1010
} from '@angular/core';
@@ -45,13 +45,13 @@ const ELEMENT_DATA: PeriodicElement[] = [
4545
styleUrls: ['table-wrapped-example.css'],
4646
templateUrl: 'table-wrapped-example.html',
4747
})
48-
export class TableWrappedExample implements OnInit {
48+
export class TableWrappedExample implements AfterViewInit {
4949
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
5050
dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
5151

52-
@ViewChild('sort', {static: true}) sort: MatSort;
52+
@ViewChild('sort') sort: MatSort;
5353

54-
ngOnInit() {
54+
ngAfterViewInit() {
5555
this.dataSource.sort = this.sort;
5656
}
5757
}

0 commit comments

Comments
 (0)