【发布时间】:2017-12-13 07:48:52
【问题描述】:
我是 Angular 4 的新手,我一直在编写分页代码。 我正在使用名为“paginationFirst()”和“paginationNext()”的单独服务分别在表中加载第一页和下一页的数据。因此,单击“第一个”选项卡时,应通过“firstPage()”函数将来自第一个服务的数据加载到表中,单击“下一步”选项卡时,应将来自第二个服务的数据加载到表中通过“nextPage()”函数。 第一页的数据加载正常,但单击下一个选项卡时未加载下一页的数据。 这两项服务都工作正常并显示正确的数据。我不想使用 npm 分页。 请在这方面提供帮助。
HTML:
<div>
<table class="table table-hover">
<thead>
<tr>
<th class="align-right">VALUE</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of calendarTableSelected">
item.value
</tr>
</tbody>
</table>
</div>
<div class="pagination" *ngIf="tableDiv">
<div>
<a href="#" class="active" (click)="firstPage()" >First</a>
</div>
<div>
<a href="#" (click)="previousPage()">Prev</a>
</div>
<div>
<a href="#" (click)="nextPage()">Next</a>
</div>
<div>
<a href="#" (click)="lastPage">Last</a>
</div>
</div>
组件.ts
firstPage(){
this.calendarService.paginationFirst().subscribe(data =>
this.calendarTableSelected = data);
}
nextPage(){
this.calendarService.paginationNext().subscribe(data =>
this.calendarTableSelected = data);
}
服务.ts
public paginationNext(){
return this.http.get(environment.serverUrl+ '/api/nextpage')
.map((responsePage:Response) => responsePage.json())
.catch((err) => {
console.log('Error while getting response from service: '+ err);
return Observable.throw(err)
})
}
public paginationFirst() {
return this.http.get(environment.serverUrl+'/api/firstpage')
.map((resService4:Response) => resService4.json())
.catch((err) => {
console.log('Error while getting response from service: '+ err);
return Observable.throw(err)
})
}
【问题讨论】:
-
我在这里有点困惑,你说“两个服务都工作正常并显示正确的数据”。如果没有为
nextPage加载数据,你怎么知道它们工作正常?而如果是“显示正确的数据”,这里有什么问题? -
@BunyaminCoskuner 这些服务是用 Python 编写的,当它们在浏览器中运行时,它们会达到所需的响应。但是从 Angular 前端,单击下一个选项卡不会显示任何数据。理想情况下,它应该用下一页服务替换表的数据。
-
好的,现在我明白了。那么,当您点击
Next按钮并触发nextPage方法时,您是否看到您的请求通过网络并返回您想要的数据(在开发人员工具上)? -
@BunyaminCoskuner 是的。我还附上了我的开发者工具的截图。请检查我刚刚为屏幕截图添加的最后一行帖子中的网址。该请求正在通过网络。但是当我点击下一步时,它会重定向到主页而不是加载数据
-
您能否在“网络”选项卡的图片中添加您的请求及其响应?如果它重定向到主页,我会假设它与您的服务器有关,而不是 Angular 应用程序。
标签: javascript html angular typescript