如何从nativescript

时间:2018-04-23 13:46:07

标签: angular typescript nativescript

我试图使用angular2 + typescript来获取点按事件的数据:

这是组件HTML:

<RadListView row="1" [items]="groceryList" [class.visible]="listLoaded" (tap)="seeItem($event)"
    swipeActions="true" (itemSwipeProgressStarted)="onSwipeCellStarted($event)">

    <ng-template let-item="item">
        <Label [text]="item.name" class="p-15 list-item"></Label>
    </ng-template>

    <GridLayout *tkListItemSwipeTemplate columns="*, auto">
        <StackLayout id="delete-view" col="1" (tap)="delete($event)" class="delete-view">
            <Image src="~/images/delete.png"></Image>
        </StackLayout>
    </GridLayout>
</RadListView>

这是组件代码:

seeItem(args: ListViewEventData) {
    console.log('item ', <Grocery>args.object.bindingContext);
    console.log('in case');
}

当我从ListView中点击一个元素时,我得到了console.log:

[Eric Guzmán’s iPhone]: item undefined
[Eric Guzmán’s iPhone]: in case

&#34;删除&#34;当点击时,event(在此组件中)使用相同的逻辑:

delete(args: ListViewEventData) {
        let grocery = <Grocery>args.object.bindingContext;
        this.groceryService.delete(grocery.id)
            .subscribe(() => {
                let index = this.groceryList.indexOf(grocery);
                this.groceryList.splice(index, 1);
            });
    }

我是NativeScript的初学者,我很感激帮助,

1 个答案:

答案 0 :(得分:-1)

如果您希望获得该项目,则需要进行以下更改

替换

<ng-template let-item="item">
        <Label [text]="item.name" class="p-15 list-item"></Label>
</ng-template>

<ng-template let-item="item">
        <Label [text]="item.name" (tap)="seeItem(item)" class="p-15 list-item"></Label>
</ng-template>

并在功能中进行更改

seeItem(item: Grocery) {
    console.log('item ', item);
    console.log('in case');
}

试试这个应该有效。