onsenui:"pushPage is already running" 和 "Cannot read property 'removeEventListener' of null"

onsenui: "pushPage is already running" and "Cannot read property 'removeEventListener' of null"

我正在使用 cordova + onsenui 开发移动应用程序。我基本上有一个通用的 template.html,我首先加载它,然后使用所需的特定参数通过 jquery 进行修改。我首先执行 ajax 请求以获取该模板的详细信息,然后使用

加载模板
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.

如果我删除修改模板的代码部分,一切正常,返回没有问题,没有错误消息,再次加载模板也没有问题。

问题是我以错误的方式修改输入, 做

$('#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>