Angular paging and javascript sending events always returns the same page

Asked

Viewed 70 times

-1

I’m studying javascript and angular now, I have a pagination component with the following HTML:

<nav aria-label="Page navigation example">
  <ul class="pagination justify-content-end">
    <li class="page-item">
      <a class="page-link" (click)="goPrevious()" tabindex="-1">Previous</a>
    </li>
    <li class="page-item">
      <a
        class="page-link"
        routerLinkActive="active"
        [routerLinkActiveOptions]="{ exact: true }"
        (click)="goToPage($event.target.value)"
        >{{ currentPage }}</a
      >
    </li>
    <li class="page-item">
      <a class="page-link" (click)="goToPage($event.target.value)">{{
        currentPage + 1
      }}</a>
    </li>
    <li class="page-item">
      <a class="page-link" (click)="goToPage($event.target.value)">{{
        currentPage + 2
      }}</a>
    </li>
    <li class="page-item">
      <a class="page-link" routerLinkActive="active" (click)="goToPage($event.target.value)">{{
        currentPage + 3
      }}</a>
    </li>
    <li class="page-item">
      <a class="page-link" (click)="goToPage($event.target.value)">{{
        currentPage + 4
      }}</a>
    </li>

    <li class="page-item">
      <a class="page-link" (click)="goNext()">Next</a>
    </li>
  </ul>
</nav>

And I have a TS component with the following functions

@Output() paginate = new EventEmitter<number>();
currentPage = 0;


  constructor() { }

  ngOnInit() { this.currentPage = 1; }

  goNext() {this.currentPage += 5; }

  goPrevious = () =>  this.currentPage > 1 ? this.currentPage -= 5 : null;

  goToPage($event: any) {
    $event = this.currentPage;
    this.paginate.emit($event);
    console.log(this.currentPage);
  }

When I click on the numbers, for example 4, it always returns me Currentpage as 1. Where I am missing ?

  • 1

    I tried that already, I didn’t.

  • pq vc does not: (click)="goToPage(currentPage + 1)"

  • I tried that too, but it didn’t work.

  • I can’t really explain it, but he always returns the front page.

  • It does so: (click)="goToPage(currentPage + 1)" which is right

2 answers

0

'a' widgets no value

(click)="goToPage(currentPage + 1)" 

ts:

 goToPage($event: any) {
    this.currentPage=$event;
    this.paginate.emit($event);
    console.log(this.currentPage);
  }

0


The problem is that I was always passing the currentPage instead of the event value. I corrected it by leaving it as follows. Then correct the method as follows:

goToPage($event: any) {
    this.paginate.emit($event.target.innerText);
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.