2017-07-30 25 views
0

我正在開發一個在cordova + onsenui上的移動應用程序。我基本上有一個通用的template.html,我第一次加載,然後通過jquery修改所需的具體參數。我第一次執行一個Ajax請求以獲取該模板的細節,然後我用onsenui:「pushPage已在運行」和「無法讀取屬性'removeEventListener'null」

fn.pushPage({'id': 'template.html', 'title': 'View template'}); 

加載模板和更改與jQuery所需要的所有參數。起初,應用程序將嘗試執行更改之前的模板將加載完成,該文檔準備事件是行不通的,所以我在的jQuery的onSuccess部分做了一個通用的功能,像這樣的

function update_template(){ 
    if ($('#templateItem').length) { 
     //this part runs if the template has loaded 
     //replace all the poarameters with jquery 
    } else { 
     window.setTimeout(edit_profile, 0.1); // 5 seconds 
    } 
}; 

這似乎工作確定,但要到前一頁與運行

的document.getElementById(「appNavigator」)後退按鈕後。popPage()

我在控制檯中的錯誤信息說

Uncaught (in promise) TypeError: Cannot read property 'removeEventListener' of null 

,如果我嘗試再次看到了模板,我得到

Uncaught (in promise) pushPage is already running. 

如果我刪除修改模板代碼的一部分,一切正常,沒有問題回去,沒有錯誤消息並沒有什麼問題再次加載模板。

回答

0

的問題是,我是在錯誤的道路正確修改輸入, 做

$('#inputNameId').html('text to replace'); 

被打破了網站的HTML,將其更改爲

$('#inputNameId').val('text to replace'); 

後一切的工作。爲了記錄在案,如果你想要的模板後的變化完成加載,你應該看看init方法,以避免在JavaScript中等待,就像這樣:

document.addEventListener('init', function(event) { 
    var page = event.target; 

    if (page.id === 'template') { 
    //code to run 
    }; 

}); 

其中模板是到這裏的模板ID:

<template id='generic.html'> 
    <ons-page id='***template***'> 
... 
    <ons-page> 
<template>