gpt4 book ai didi

angular - Ionic2 ion-toggle 在 ionChange 上获得值(value)

转载 作者:太空狗 更新时间:2023-10-29 17:03:42 26 4
gpt4 key购买 nike

我这里有这个开关:

<ion-toggle (ionChange)="notify(value)"></ion-toggle>

当我单击它时,我想调用方法 notify 通过参数传递切换值。如何获取切换值?

谢谢!

最佳答案

就像你在Ionic2 Docs - Toggle中看到的一样,更好的方法是使用 ngModel 从组件中将切换绑定(bind)到属性

组件代码:

public isToggled: boolean;

// ...

constructor(...) {
this.isToggled = false;
}

public notify() {
console.log("Toggled: "+ this.isToggled);
}

查看:

<ion-toggle [(ngModel)]="isToggled" (ionChange)="notify()"></ion-toggle>

这样,您就不需要发送该值,因为它已经是您组件的一个属性,并且您始终可以使用 this.isToggled 获取该值.

更新

就像 @GFoley83 在他的评论中提到:

Be very careful with ionChange when it's bound directly to ngModel. The notify method will get called when the value bound to ngModel changes, either from the view via someone clicking the toggle or in your component via code e.g. when you do a fresh GET call. I had an issue recently whereby clicking the toggle triggered a PATCH, this meant that every other client would automatically trigger a PATCH when the data bound to the toggle changed.

We used:<ion-toggle [ngModel]="isToggled" (ngModelChange)="notifyAndUpdateIsToggled()"></ion-toggle> to get around this

关于angular - Ionic2 ion-toggle 在 ionChange 上获得值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40729335/

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