最佳答案
您无法通过 $index 访问索引,因为 Repeater 没有实现这样的逻辑。您可以为每个数组项创建自己的 id,然后将其用作索引。 例如
页面.ts
items.forEach(element => {
element.id = id;
id++;
});
页面.xml
<Repeater items="{{ items }}">
<Repeater.itemsLayout>
<StackLayout />
</Repeater.itemsLayout>
<Repeater.itemTemplate>
<Label text="{{ id }}" textWrap="true" />
</Repeater.itemTemplate>
</Repeater>
然后您可以使用被点击 View 的绑定(bind)上下文来获取被点击的特定项目。
exports.onTap = function(args) {
var item = args.view.bindingContext;
console.log('item.id', item.get('id'));
};
关于javascript - $index 在 Nativescript 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41670955/