可以说我有以下的getter/setter方法如何从模板绑定中的角度4访问getter/setter访问器?
get next() {
console.log(this.people[this._index], this._index);
return this.people[this._index];
}
set next(i: any) {
this._index = (+i) + 1;
this._index = (+i) % this.people.length;
}
,我想通过以下方式来调用这个:
<ng-template ngFor let-person="$implicit" [ngForOf]="people" let-i=index let-last=last>
<app-card [cardItem]="people[i]" [nextCard]="next(i)"></app-card>
</ng-template>
PS :把它想象成圆形阵列。在哪里我需要prev,当前和下一个项目。
不过,我得到以下错误
角:会员 '下一步',在不赎回
这是为什么?什么是解决方案?
感谢
编辑
谢谢你们的帮助和解释。有了您的帮助,我设法使其工作:
<app-card [currentCard]="people[i]" [nextCard]="people[i === people.length - 1 ? 0: i + 1]" [prevCard]="i == 0 ? people[people.length - 1] : people[i - 1]"></app-card>
因此,它非常圆阵。 让我们假设我们有以下几点:
people["James Dan", "Aluan Haddad", "Jota Toledo"]
这么几个条件:
- 如果我站在阵列(即
index = 0
)的开头 - 那么我prev
将people[people.length - 1]
这是数组中的最后一个元素。如果我现在是在索引1,那么我的分组将是指数0,接下来的将是指数2
我增加了一个例子,我的答案。这显示了如何正确使用属性,并演示如何在保持模板复杂性的同时构建显示结构。 –
谢谢@AluanHaddad – James