javascript - $index 在 Nativescript 中不起作用

标签 javascript xml nativescript

我一直在寻找解决方案,但没有运气,我试图使用 observablearray 的索引,但控制台总是抛出错误,并且渲染的 View 除了标签上的空白外不显示任何内容 {{ $index }} 被写入。

enter image description here

最佳答案

您无法通过 $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/

相关文章:

javascript - Promise 模式 - 使用 Promise.all()

javascript - 将大小和名称传递给 JavaScript 的 FileHandler 的 .onload 事件

javascript - 我的正则表达式在 regex101.com 沙箱中有效,但在实际的 javascript 中无效

xml - XPath 2.0 - 1.0 等效?

xml - XSLT - 不能在复杂的图形表示中只复制一次元素

angular - native 脚本 Angular : How do I bind other pages into a TabView?

javascript - Sinon 不 Mocking db-mysql Node.js 库

android - 无法运行 Hello world

android - Nativescript:带有图标和多行标题的按钮

nativescript - nativescript 分段栏中的图像图标