Manson Mamaril
Manson Mamaril

Reputation: 153

$index not working in Nativescript

I've been searching for solutions with no luck, I'm trying to use the index of the observablearray but the console always throws and error and the rendered view doesn't display anything but blank on the label the {{ $index }} is written.

enter image description here

Upvotes: 0

Views: 186

Answers (1)

Nick Iliev
Nick Iliev

Reputation: 9670

You can not access the index via $index as there is no such logic implemented for Repeater. You can create your own id for each of your array items and then use it as an index. e.g.

page.ts

items.forEach(element => {
    element.id = id;
    id++;
});

page.xml

<Repeater items="{{ items }}">
    <Repeater.itemsLayout>
        <StackLayout />
    </Repeater.itemsLayout>
    <Repeater.itemTemplate>
        <Label text="{{ id }}" textWrap="true" />
    </Repeater.itemTemplate>
</Repeater>

And then you can get the specific item tapped with the bindingContext of the tapped view.

exports.onTap = function(args) {
    var item = args.view.bindingContext;
    console.log('item.id', item.get('id'));
};

Upvotes: 1

Related Questions