本文介绍了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开关可防止在初始绑定时触发更改事件
发布评论