Angular Material MatPaginator showing page 0 of 0

sorry for the question in advance. I googled my problem before writing this and tried lots of solutions, but without luck. I'm an Angular noob trying to learn Material. My simple project makes a http request and uses the JSON received to populate a table. The Angular Material table is working fine, but the paginator is not. It is greyed and shows page 0 of 0.

The component.html is as follows:

<div style="height: 80vh;">
  <mat-toolbar color="primary">
      <button mat-icon-button (click)="sidenav.toggle()" fxShow="true">
      <span>Teste - Requisição Http/Tabela</span>
      <span class="menu-spacer"></span>

  <mat-sidenav-container fxFlexFill>
    <mat-sidenav #sidenav>
        <a (click)="sidenav.toggle()" mat-list-item>Fechar</a>
        <a href="#" mat-list-item>Link 1</a>
        <a href="#" mat-list-item>Link 2</a>
        <a href="#" mat-list-item>Link 3</a>
    <mat-sidenav-content fxFlexFill>
      <table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

        <ng-container matColumnDef="id">
          <th mat-header-cell *matHeaderCellDef> Id </th>
          <td mat-cell *matCellDef="let element"> {{}} </td>

        <ng-container matColumnDef="email">
          <th mat-header-cell *matHeaderCellDef> Email </th>
          <td mat-cell *matCellDef="let element"> {{}} </td>

        <ng-container matColumnDef="first_name">
          <th mat-header-cell *matHeaderCellDef> First Name </th>
          <td mat-cell *matCellDef="let element"> {{element.first_name}} </td>

        <ng-container matColumnDef="last_name">
          <th mat-header-cell *matHeaderCellDef> Last Name </th>
          <td mat-cell *matCellDef="let element"> {{element.last_name}} </td>

        <ng-container matColumnDef="avatar">
          <th mat-header-cell *matHeaderCellDef> Avatar </th>
          <td mat-cell *matCellDef="let element"> {{element.avatar}} </td>

        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
      <mat-paginator [pageSizeOptions]="[5, 10, 25, 100]" showFirstLastButtons></mat-paginator>

The component.ts is as follows:

import { HttpClient } from '@angular/common/http';
import { AfterViewInit, Component, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatTableDataSource } from '@angular/material/table';

export interface UserData {
  id: number;
  email: string;
  first_name: string;
  last_name: string;
  avatar: string;

const USER_DATA: UserData[] = [];

  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
export class AppComponent implements AfterViewInit {
  title = 'Test';
  url = '';
  displayedColumns: string[] = ['id', 'email', 'first_name', 'last_name', 'avatar'];
  dataSource = new MatTableDataSource<UserData>(USER_DATA);
  constructor(private http: HttpClient) {
    this.http.get(this.url).toPromise().then(data => {
      this.dataSource = data["data"]

  @ViewChild(MatPaginator) paginator: MatPaginator;

  ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;

What am I doing wrong? Thanks

1 answer

  • answered 2020-12-01 21:45 Becike

    In the angular's documentation, they forgot to add, that you need to define { static: false} or {static: true} .

    Like this:

    @ViewChild(MatPaginator, {static:false}) paginator: MatPaginator;

    in docs:

    static - True to resolve query results before change detection runs, false to resolve after change detection. Defaults to false.

    You need to set it to false, if you want to use default.