In NativeScript with Angular2 get element value

前端 未结 1 796
名媛妹妹
名媛妹妹 2021-01-26 13:40

I have a list, and I want to get the value of the list item.

The view is as follows

         


        
相关标签:
1条回答
  • 2021-01-26 14:26

    You can access the view of your item template via args.view. From that point, I assume that you will have different text in your list-items so it is important to create unique IDs for each Label via binding(using the Angular index). So you can do the following:

    <ListView  [items]="myItems" (itemTap)="onItemTap($event)">
        <template let-item="item" let-i="index" let-odd="odd" let-even="even">
            <StackLayout [class.odd]="odd" [class.even]="even">
                <Label [id]="'lbl' + i" [text]='"Value is: " + i'></Label>
            </StackLayout>
        </template>
    </ListView>
    

    and then in your onItemTap

    public onItemTap(args: ItemEventData) {
        console.log("Item Tapped at cell index: " + args.index);
        console.log(args.object); // prints something like ListView(137)
        console.log(args.view); // prints something like StackLayout(265)
    
        var lbl = <Label>args.view.getViewById("lbl" + args.index);
    
        console.log(lbl.text); // prints the actual text of the tapped label
    }
    
    0 讨论(0)
提交回复
热议问题