简体   繁体   English

增量在上一个,下一个按钮Angular 4中不起作用

[英]Incrementing doesn't work in prev, next buttons Angular 4

I had to create a prev and next component to go to the next page using router from Angular. 我必须使用Angular的路由器创建一个上一个和下一个组件才能转到下一页。 The issue is not about angular, but about the incrementing and decrementing logic. 问题不关乎角度,而是关乎递增和递减逻辑。

When I click next first time is ok, but second time I have to click twice to see the next page. 第一次单击时可以,但是第二次单击时必须两次才能看到下一页。 The same is happening with prev button. 上一个按钮也是如此。 If I want to click back, after I clicked first next, I have to click twice to go to prev page. 如果要单击返回,请先单击两次,然后单击两次以转到上一页。

I looked and I looked and don't see where is the bug. 我看了看,没看错在哪里。

HTML file HTML文件

<button class="btn btn-primary" (click)="prevPage()">Prev</button>  
<button class="btn btn-primary" (click)="nextPage()">Next</button>

TS file TS文件

pages = [
 {name: '../page1', index: 0}, 
 {name: '../page2', index: 1}, 
 {name: '../page3', index: 2}
];
current = this.pages[0];

getIndex(currentIndex: number, shift: number){
 const lenght = this.pages.length;
 const incre = (((currentIndex + shift) + lenght) % lenght);
 console.log(incre);
 return incre;
}

prevPage() {
 const i = this.getIndex(this.current.index, -1);
 this.current = this.pages[i];
 this.router.navigate([this.current.name], { relativeTo: this.route });
 console.log(this.current.name);  
}

nextPage() {
 const i = this.getIndex(this.current.index, 1);
 this.current = this.pages[i];
 this.router.navigate([this.current.name], { relativeTo: this.route });
 console.log(this.current.name);   
}

in the HTML file. 在HTML文件中。

<button class="btn btn-primary" (click)="prevPage()">Prev</button>  
<button class="btn btn-primary" (click)="nextPage()">Next</button>

change the (click) listener to nextPage(). 将(单击)侦听器更改为nextPage()。

plus, it is not make sense the ( + length) in 另外,在(+长度)中没有意义

const incre = (((currentIndex + shift) + lenght) % lenght); const incre =((((currentIndex + shift)+长度)%长度);

it will work fine without it!. 没有它,它将正常工作!

const incre = ((currentIndex + shift) % lenght); const incre =(((currentIndex + shift)%长度);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM