How to retain json objects in multiple ons-template pages in onsenui?

点点圈 提交于 2019-12-12 04:47:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!