问题
Here I am populating json objects in ajax. In navigator page, it retains all the json objects. But when I switch to another template (page2.html), it doesn't retain the json objects. I need to populate the userid, acc_nbr(mentioned in the code) in other ons-template pages.
Any help regarding this?
Here is my code.
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "Controller?token=123",
dataType: 'json',
success: function(data){
$('#userid').html(data.userid);
$('#acc_nbr').html(data.accountnbr);
},
error:function(){
alert("Sorry!");
}
});
});
</script>
index.html
<ons-navigator animation="slide" var="app.navi">
<ons-page>
/*/ content /*/
<div id="userid"></div>
</ons-page>
</ons-navigator>
<ons-template id="page2.html">
<ons-page>
/*/ content /*/
<div id="acc_nbr"></div>
</ons-page>
</ons-template>
回答1:
Try adding this to your <script>
section
$(document.body).on("pageinit","#page2.html", function() {
// call that same ajax function
}
来源:https://stackoverflow.com/questions/26377268/how-to-retain-json-objects-in-multiple-ons-template-pages-in-onsenui