0

Angular material paginator has a method starting with _.

_changePageSize(pageSize: number) {
    // Current page needs to be updated to reflect the new page size. Navigate to the page
    // containing the previous page's first item.
    const startIndex = this.pageIndex * this.pageSize;
    const previousPageIndex = this.pageIndex;

    this.pageIndex = Math.floor(startIndex / pageSize) || 0;
    this.pageSize = pageSize;
    this._emitPageEvent(previousPageIndex);
  }

This method is not listed in the Paginator API documentation. However it's refered in answers like this.

In the same paginator component, a method defined as private and not available in IDE.

private _updateDisplayedPageSizeOptions() {
    if (!this._initialized) { return; }

    // If no page size is provided, use the first page size option or the default page size.
    if (!this.pageSize) {
      this._pageSize = this.pageSizeOptions.length != 0 ?
          this.pageSizeOptions[0] :
          DEFAULT_PAGE_SIZE;
    }

In AOT it gives error after successful compilation.

i 「wdm」: Compiled successfully.

ERROR in src/app/models/models.component.ts(153,22):

error TS2341: Property '_updateDisplayedPageSizeOptions' is private and only accessible within class 'MatPaginator'.

I understand _ by conventions refers private and no direct private method concept in Javascript. Just need more info on:

  • Is that an internal method that should not be relied on?
  • In angular (or in angular-material) what is the difference between a method that starts with _ and regular method?
  • If it's changePageSize is not a private method why _ suffix? If it's private method, why it's not marked as private? Is this inconsistency or something I'm missing here?
Jones
  • 548
  • 15
  • 33

2 Answers2

0

Yes - the underscore prefix is a common programming convention for private members. Anything that isn't part of the public API could change or be removed in a future version (maybe even the very next one) and therefore isn't reliable. Public API changes would at least be subject to semantic versioning rules and show up in the changelog.

There is no difference between a function that has an underscore prefix and one that doesn't except for the prefix itself. It's the intended use of the component and its functions etc. that is the concern, and the underscore is used to identify members that are not intended for use outside the component.

G. Tranter
  • 16,766
  • 1
  • 48
  • 68
  • Added one more API with private access specifier from the same class. Isn't that implying `_changePageSize` is not private? – Jones Nov 29 '19 at 07:47
  • "API" doesn't automatically mean public. An underscore does not make/unmake an API member "private/public" with respect to the API description - it is nothing more than a code convention. **Public** API is defined by the software provider through documentation which is a kind of guarantee/contract for the software version. Read about semantic versioning https://semver.org/ which applies to **"public"** API. For Paginator - https://material.angular.io/components/paginator/api#MatPaginator is the **public** API. Anything outside of that is not guaranteed by the the rules of semantic versioning. – G. Tranter Nov 30 '19 at 18:29
0

If your question is: "Should I do the same in my code?" Then no, use private changePageSize(){...} - if you're worried about safety, let TypeScript help you, that's its purpose.

If you're wondering about convention, then it's not advised to use this function as the API makes no guarantees that will will work the same way, or even exist in future updates.

Richard Dunn
  • 6,165
  • 1
  • 25
  • 36
  • But there is no `changePageSize()` method! – Jones Nov 29 '19 at 04:25
  • _"Should I do the same in my code?"_ is supposed to read: _"If I were to create a private function, what's the best way to declare it?"_. Sorry for the confusion. – Richard Dunn Nov 29 '19 at 11:52