【发布时间】:2017-09-04 19:10:11
【问题描述】:
我有一个分页器组件来显示页面列表并检测用户何时单击以显示另一个页面并且该组件不更改页面仅必须触发事件但我无法观看更改事件。
该组件有一个双向绑定来宣布当前页面已更改并且可以正常工作,因为我可以在更改后在 html 中显示该值(请参阅
在 HTML 中)。
在分页器组件中更改当前页面后如何执行页面更改?
我阅读了以下https://stackoverflow.com/questions/36150107/angular-2-change-event-model-changes 但不适用于我的情况。
HTML(使用分页器):
<paginator [pageCount]=pageCount [(currentPage)]=currentPage (ngModelChange)="pageChanged($event)"></paginator> <!--pageChanged is not called-->
<p>{{currentPage}}</p> <!--Work Fine showing the new page selected!-->
TS(分页器组件):
import { Component, OnInit, OnChanges, SimpleChanges, Input, Output, EventEmitter, ChangeDetectorRef } from '@angular/core';
@Component({
selector: 'paginator',
templateUrl: './paginator.component.html',
styleUrls: ['./paginator.component.css']
})
export class PaginatorComponent implements OnInit, OnChanges {
pageList : number[] = [1];
@Input() pageCount : number = 1;
@Input() currentPage : number = 1;
@Output() currentPageChange = new EventEmitter<number>();
constructor(private ref:ChangeDetectorRef) {}
ngOnInit()
{
this.loadPageList();
}
ngOnChanges(changes: SimpleChanges)
{
this.loadPageList();
}
changePage(page : number)
{
if(page < 1)
return;
if(page > 1 && page > this.pageCount)
return;
if(this.currentPage == page)
return;
this.currentPage = page;
this.currentPageChange.emit(this.currentPage);
}
private loadPageList()
{
if(this.pageCount < 1)
this.pageCount = 1;
let pages = [1];
for(let p = 2; p <= this.pageCount; p++)
pages.push(p);
this.pageList = pages;
this.ref.detectChanges();
}
}
HTML(分页器组件):
<div class="row">
<div class="col-md-12 text-center">
<ul class="pagination">
<li><a (click)="changePage(1)">«</a></li>
<li><a (click)="changePage(currentPage - 1)">❮</a></li>
<li *ngFor="let page of pageList" [class.active]="currentPage==page">
<a id="changePage{{page}}" (click)="changePage(page)">{{page}}</a>
</li>
<li><a (click)="changePage(currentPage + 1)">❯</a></li>
<li><a (click)="changePage(pageCount)">»</a></li>
</ul>
</div>
</div>
【问题讨论】:
标签: angular