diff --git a/src/material/paginator/paginator.spec.ts b/src/material/paginator/paginator.spec.ts index 341ade9da951..ad01f4f3eddf 100644 --- a/src/material/paginator/paginator.spec.ts +++ b/src/material/paginator/paginator.spec.ts @@ -413,6 +413,31 @@ describe('MatPaginator', () => { expect(getLastButton(fixture).hasAttribute('disabled')).toBe(true); }); + it('should emit the `page` event when the pageIndex is changed', () => { + paginator.pageIndex = 2; + + expect(component.pageEvent).toHaveBeenCalledWith(jasmine.objectContaining({ + previousPageIndex: 0, + pageIndex: 2 + })); + }); + + it('should emit the `page` event when the length is changed', () => { + paginator.length = 1337; + + expect(component.pageEvent).toHaveBeenCalledWith(jasmine.objectContaining({ + length: 1337 + })); + }); + + it('should emit the `page` event when the pageSize is changed', () => { + paginator.pageSize = 27; + + expect(component.pageEvent).toHaveBeenCalledWith(jasmine.objectContaining({ + pageSize: 27 + })); + }); + }); function getPreviousButton(fixture: ComponentFixture) { diff --git a/src/material/paginator/paginator.ts b/src/material/paginator/paginator.ts index 4a3becd0fb90..5aa3d0cb5737 100644 --- a/src/material/paginator/paginator.ts +++ b/src/material/paginator/paginator.ts @@ -90,8 +90,14 @@ export class MatPaginator extends _MatPaginatorBase implements OnInit, OnDestroy @Input() get pageIndex(): number { return this._pageIndex; } set pageIndex(value: number) { - this._pageIndex = Math.max(coerceNumberProperty(value), 0); - this._changeDetectorRef.markForCheck(); + const newPageIndex = Math.max(coerceNumberProperty(value), 0); + const oldPageIndex = this._pageIndex; + + if (newPageIndex !== oldPageIndex) { + this._pageIndex = newPageIndex; + this._emitPageEvent(oldPageIndex); + this._changeDetectorRef.markForCheck(); + } } private _pageIndex = 0; @@ -99,8 +105,13 @@ export class MatPaginator extends _MatPaginatorBase implements OnInit, OnDestroy @Input() get length(): number { return this._length; } set length(value: number) { - this._length = coerceNumberProperty(value); - this._changeDetectorRef.markForCheck(); + const newLength = coerceNumberProperty(value); + + if (newLength !== this._length) { + this._length = newLength; + this._emitPageEvent(this.pageIndex); + this._changeDetectorRef.markForCheck(); + } } private _length = 0; @@ -108,8 +119,13 @@ export class MatPaginator extends _MatPaginatorBase implements OnInit, OnDestroy @Input() get pageSize(): number { return this._pageSize; } set pageSize(value: number) { - this._pageSize = Math.max(coerceNumberProperty(value), 0); - this._updateDisplayedPageSizeOptions(); + const newPageSize = Math.max(coerceNumberProperty(value), 0); + + if (newPageSize !== this._pageSize) { + this._pageSize = newPageSize; + this._updateDisplayedPageSizeOptions(); + this._emitPageEvent(this.pageIndex); + } } private _pageSize: number;