【发布时间】:2017-03-31 18:07:11
【问题描述】:
所以我开始使用 Angular2 并尝试通过 ng2-pagination 组件添加分页,经过大量试验和错误后,我已经达到我的数据正确显示的阶段,分页控件显示正确但页面更改没有被解雇,所以我总是留在第一页。
这是我的html
<div id="item_box" *ngFor="let item of listItems | paginate:
{itemsPerPage: 3, currentPage: page};">
<! -- Elements to display my data items, all works OK -->
</div>
<div id="pager" class="pagination">
<pagination-template #p="paginationApi" (pageChange)="pageChange.emit($event)">
<ul>
<li [class.disabled]="p.isFirstPage()">
<a (click)="p.setCurrent(1)">First</a>
</li>
<li [class.disabled]="p.isFirstPage()">
<a (click)="p.previous()">Previous</a>
</li>
<li *ngFor="let page of p.pages" [class.current]="p.getCurrent() === page.value">
<a (click)="p.setCurrent(page.value)">{{page.value}}</a>
</li>
<li [class.disabled]="p.isLastPage()">
<a (click)="p.next()">Next</a>
</li>
<li [class.disabled]="p.isLastPage()">
<a (click)="p.getLastPage()">Last</a>
</li>
</ul>
</pagination-template>
</div>
视觉上一切看起来都很好。
任何我的 list.component 代码
import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core';
import { Subscription } from 'rxjs';
import { Router, ActivatedRoute, Params } from "@angular/router";
import { PaginationModule } from 'ng2-bootstrap/components/pagination';
@Component({
selector: 'app-list',
templateUrl: './list.component.html',
styleUrls: ['./list.component.css']
})
export class ListComponent implements OnInit, OnDestroy {
listItems = [];
private subscription: Subscription;
@Input() id: string;
@Input() page;
@Input() maxSize: number;
@Output() pageChange: EventEmitter<number> = new EventEmitter<number>();
constructor(private apiService:ApiService, private router:Router, private activatedRoute: ActivatedRoute)
{
}
ngOnInit()
{
//Code removed for brevity, but here I execute WS and get data here, populated in this.listItems
this.listItems == result;
...
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
我没有收到任何错误。但是当我点击更改页面时,什么也没有发生。
我确信它一定很简单,我在某个地方错过了一些东西,但是什么和在哪里?
https://github.com/michaelbromley/ng2-pagination 上的示例/代码没有帮助,我发现的任何其他示例也没有帮助。
有什么想法吗?我做错了什么?
【问题讨论】:
-
嗨。我是lib作者。问题:在 PaginatePipe 中,您设置了
currentPage: page- 我在您的代码中没有看到“页面”变量。它是作为输入传递的吗?目前,您的代码似乎只是重新发出 pageChange 事件,但我看不到任何会实际更改“page”值的部分 - 这必须发生才能使分页正常工作。 -
嗨,迈克尔,是的,我在课堂上有过它,但在上面的代码中错过了它(复制过去的问题)。但这为我指明了正确的方向,我已经解决了这个问题。请参阅我在下面发布的答案
标签: angular pagination