gpt4 book ai didi

javascript - ionic 框架 - 无法阻止 ion-toggle 切换

转载 作者:行者123 更新时间:2023-11-28 04:28:03 27 4
gpt4 key购买 nike

我在我的应用程序中使用 ion-toggle 。当用户单击(检查) ion-toggle 时,我在 ion-toogle 上编写了 ionChange Hook 。在此 Hook 中,由于应用程序逻辑,我提示用户无法设置 ion-toggle 。

使用 event.preventDefault 防止 ion-toggle 切换

组件 html

<ion-content padding>
<ion-slides pager="true">
<ion-slide *ngFor="let giftSlide of giftsAllMatrix" >
<ion-row *ngFor="let giftRecord of giftSlide">
<ion-col *ngFor="let giftCol of giftRecord" col-6>
<img src="./assets/{{giftCol.giftImage}}" >
<p> gift code : {{ giftCol.giftCode }} </p>
<ion-item>
<ion-toggle #giftSelect (ionChange)="onGiftSelect(giftCol.giftCode, giftCol.pointsRequired ,giftCol.giftImage,giftSelect.checked,giftSelect,$event)" checked="false"></ion-toggle>
</ion-item>
</ion-col>
</ion-row>
</ion-slide>
</ion-slides>

组件钩子(Hook) - onGiftSelect

onGiftSelect( giftSelCode : string , 
pointsRequired : number ,
giftImage : string ,
selected : boolean
,giftSelect : any,
event : Event)
{
console.log("inside onGiftSelect "+giftSelCode);
console.log("inside onGiftSelect points required "+pointsRequired);
console.log(" selected "+selected );
console.log(" this.giftSelectedFlg "+this.giftSelectedFlg );
let navigate = true;
// if ion-toggle is true then only navigate
// when toggle is from 'selected' to 'deselected' do not
if ( selected === true)
{
if ( this.giftSelectedFlg === false )
{
this.giftSelectedFlg = true;
}
else
{

if(isProdSetup() === true)
{
this.toast.show( "Select one gift at time ", '3000', 'center').subscribe
( toast =>
{
console.log(toast);
});
}
else
{
let alert = this.alertCtrl.create({
title: 'Gift selection',
subTitle: 'Multiple gift selection disallowed',
buttons: ['Dismiss']
});
alert.present();
}
navigate = false;
event.preventDefault();
}
if ( navigate)
{
// Pass the control to giftSelect page
this.navCtrl.push("Giftselect",
{
giftCode : giftSelCode ,
pointsRequired : pointsRequired ,
giftImage : giftImage
});
}

}

使用 viewchild

使用nativeElement

这段代码也不起作用。任何人都可以帮助我找到正确的方法来防止 ion-toggle 组件阻止 ionChange 事件中的切换。

 export class Giftfactory {
giftsAllMatrix : GiftCatalogueEntry[][][];
gifts : Array<any> ;
totalPoints : number = 0;
error : string;
giftSelectedFlg : boolean = false;
giftChoice : any;
//giftSelect : any; //template variable.
@ViewChild ('giftSelect') gtSel ;

onGiftSelect( giftSelCode : string ,
pointsRequired : number ,
giftImage : string ,
selected : boolean
,giftSelect : any,
event : Event)
{
...
// trying to uncheck ion-toggle - does not work
this.gtSel.nativeElement.checked = false;
}

最佳答案

添加一个@click监听器(除了@ionChange监听器)并在点击处理程序中运行event.stopImmediatePropagation()这对我来说是个窍门!它阻止了切换,并允许我以编程方式设置其值。

关于javascript - ionic 框架 - 无法阻止 ion-toggle 切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44869282/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com