我正在开发离子2的应用程序,但我遇到点击事件的问题。
当我在设备上运行应用程序并尝试单击按钮例如发出警报时,此功能会触发一次,但是当我再次单击该按钮时,该功能会触发两次。
这是系统信息。
Cordova CLI: 6.5.0
Ionic Framework Version: 3.0.1
Ionic CLI Version: 2.2.2
Ionic App Lib Version: 2.2.1
Ionic App Scripts Version: 1.3.0
ios-deploy version: 1.9.0
ios-sim version: 5.0.8
OS: macOS Sierra
Node Version: v7.2.1
Xcode version: Xcode 8.3.2 Build version 8E2002
这是示例代码:
home.htm
<div padding>
<button ion-button full (click)="TestAlert()">Alert</button>
</div>
home.ts
TestAlert(){
console.log('Hola');
alert('Hola');
}
这是我正在做的repo
的完整示例答案 0 :(得分:7)
尝试使用(tap)
代替(click)
。
(因为您正在使用离子原生3.0.1,因此更改该点击会干扰滚动,如果发生这种情况,您需要更新到3.1.1)。
答案 1 :(得分:-3)
HTML
<div padding>
<button type="button" class="ion-button full">Alert</button>
</div>
JS
// wait until all dom elements are loaded
$(document).ready(function(){
// init TestAlert as Listener
var TestAlert = function(){
console.log('Hola');
alert('Hola');
}
// bind the click event to the button
$('button').on('click', TestAlert);
});