2017-05-04 29 views
2

我正在開發離子2的應用程序,但是我遇到了點擊事件的問題。離子2(點擊)觸發兩次iOS 10.3.1

當我在設備上運行應用程序,並嘗試點擊一個按鈕例如作出警報,此功能會觸發一次,但當我再次點擊按鈕時,該功能會觸發兩次。

這是系統信息。

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

這是因爲btn默認有一個點擊偵聽器,並且您添加了第二個偵聽器。嘗試使js中的點擊呼叫不在html中。 – TypedSource

回答

5

嘗試使用的(tap)代替(click)

(因爲您使用的是離子本機3.0.1,有一個變化,即tap會干擾滾動,如果發生這種情況,您需要更新到3.1.1)。

+0

謝謝,你爲我節省了很多時間。 –

+0

謝謝你的工作。如果我的iOS應用程序功能被調用兩次。例如:如果我從ionViewDidLoad()調用函數this.make_shortlisted(),它將被調用兩次,這會爲我創建頁面加載延遲。 – Vasanth

-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); 
}); 
+0

這裏的問題是離子框架是基於angular2,我不使用jQuery我使用打字稿。 –

+0

你可以用香草js重寫我的代碼,我只是懶惰的,因爲它更快,所以使用jquery。 – TypedSource

+0

jquery更快,hahaha ,, – Anuj