From 7b603c5691e275b235b9290c547e201cfd5fad67 Mon Sep 17 00:00:00 2001 From: Guillaume de Jabrun Date: Mon, 1 Apr 2019 18:33:19 +0200 Subject: [PATCH] fix(schematics): use ngAfterViewInit instead of ngOnInit in table (#15446) According to the offical doc of Angular, view queries are set before the ngAfterViewInit callback is called. https://angular.io/api/core/ViewChild ngOnInit works with the current version of Angular, but it can change in the future. --- .../__name@dasherize__.component.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/schematics/ng-generate/table/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts b/src/lib/schematics/ng-generate/table/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts index 65be705a324..f1478b1a60d 100644 --- a/src/lib/schematics/ng-generate/table/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts +++ b/src/lib/schematics/ng-generate/table/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit, ViewChild<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(changeDetection !== 'Default') { %>, ChangeDetectionStrategy<% }%> } from '@angular/core'; +import { AfterViewInit, Component, ViewChild<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(changeDetection !== 'Default') { %>, ChangeDetectionStrategy<% }%> } from '@angular/core'; import { MatPaginator, MatSort } from '@angular/material'; import { <%= classify(name) %>DataSource } from './<%= dasherize(name) %>-datasource'; @@ -15,7 +15,7 @@ import { <%= classify(name) %>DataSource } from './<%= dasherize(name) %>-dataso encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>, changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %> }) -export class <%= classify(name) %>Component implements OnInit { +export class <%= classify(name) %>Component implements AfterViewInit { @ViewChild(MatPaginator) paginator: MatPaginator; @ViewChild(MatSort) sort: MatSort; dataSource: <%= classify(name) %>DataSource; @@ -23,7 +23,7 @@ export class <%= classify(name) %>Component implements OnInit { /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */ displayedColumns = ['id', 'name']; - ngOnInit() { + ngAfterViewInit() { this.dataSource = new <%= classify(name) %>DataSource(this.paginator, this.sort); } }