Nativescript开关可防止在初始绑定时触发更改事件

编程入门 行业动态 更新时间:2024-10-27 11:25:54
本文介绍了Nativescript开关可防止在初始绑定时触发更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的模板如下图所示

<ListView [items]="modules"> <template let-item="item" > <StackLayout orientation="vertical"> <Switch (checkedChange)="onSwitchModule(item.id,$event)" [checked]="item.active"></Switch> </StackLayout> </template> </ListView>

我的控制器是

ngOnInit() { this._moduleService.getUserModules() .subscribe( response=>{ this.modules = response.data; } ) } onSwitchModule(itemId) { console.log(itemID); //Gets called on initial true binding on switch checked }

每次在页面上加载item.active时都会调用onSwitchModule,在任何项目上都为true,如何处理呢?

The onSwitchModule get called everytime the page loads with item.active is true on any item, how to handle this ?

注意:Nativescript的初学者

NOTE: Beginner in Nativescript

推荐答案

我要克服的问题是,我注意的是tap事件而不是checkedChange:

What I did to overcome this is I watch for tap events instead of checkedChange:

<Switch (tap)="switchClicked" [checked]="item.active"></Switch>

在回调中,您可以从bindingContext获取当前项目:

and in the callback, you can get the current item from bindingContext:

function switchClicked(args) { const item = args.object.bindingContext.item; }

更多推荐

Nativescript开关可防止在初始绑定时触发更改事件

本文发布于:2023-11-04 01:27:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556571.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可防止   事件   Nativescript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!