0
我有一个数组,我想一次只显示数组中的一个对象。一旦我有一个对象显示我然后想通过按钮循环访问该数组。我能够让数组显示,但我无法弄清楚如何一次只显示一个对象。这是我迄今为止的plunker。通过阵列角度2的循环
我不确定在这种情况下我是否正确使用*ngFor
。谢谢你的帮助!
我有一个数组,我想一次只显示数组中的一个对象。一旦我有一个对象显示我然后想通过按钮循环访问该数组。我能够让数组显示,但我无法弄清楚如何一次只显示一个对象。这是我迄今为止的plunker。通过阵列角度2的循环
我不确定在这种情况下我是否正确使用*ngFor
。谢谢你的帮助!
@Component({
selector: 'my-app',
template: `
<div>
<div>{{index}}</div>
<h2>{{item[index].title}}</h2>
<button (click)="Next()">next</button>
</div>
`,
})
export class App {
public item = ITEM;
constructor() {}
index = 0;
Next(id){
this.index ++;
if(this.index >= this.item.length) {
this.index = 0;
}
}
}
@Component({
selector: 'my-app',
template: `
<div>
<h2 *ngFor="let myItems of items">{{myItems.title}}</h2>
{{index}}
<button (click)="next()">next</button>
</div>
`,
})
export class App {
public item = ITEM;
public index = 0;
public get items() {
return this.item.filter((item, index) => index <= this.index);
}
constructor() {}
public next(){
this.index = Math.min(++this.index, this.item.length - 1);
}
}
你不需要ngFor显示单个值。所有你需要的是你想显示的索引:{{myArray [theDisplayedIndex]}}。现在用你的按钮增加显示索引,并且你有解决方案。 –
请在问题中提供[mcve]中的所有相关代码,而不是在第三方网站上。 –