Issue while getting data from one XML View Controller to Another view controller in sapui5

帅比萌擦擦* 提交于 2019-12-12 04:02:30

问题


I have to use the one xml view controller methods into another XML View controller. Here is an example (controller files only):

**view1.js**
sap.ui.controller("sap.ui.xml.view.View1", {
  onInit: function() {
    this.globalCount = 0;
    loadedView1 = this.getView(); //loadedViews is a global variable which is defined in component.js
  },
  increaseCount: function(){
   this.globalCount++;
  }
});

**view1.xml**
<core:View xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc"
    xmlns="sap.m" xmlns:l="sap.ui.layout" xmlns:c="sap.ui.commons" xmlns:f="sap.ui.layout.form"
    controllerName="sap.ui.fisa.view.OrderDetails" xmlns:html="http://www.w3.org/1999/xhtml">
    <Page showNavButton="true"  >
     <content>
        <Label text="example text" />
     </content>
    </Page>

Second View:

**view2.js**
    sap.ui.controller("sap.ui.xml.view.View2", {
      onInit: function() {

         this.view2Count = loadedView1.getController().globalCount ;

      }
    });

    **view2.xml**
    <core:View xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc"
        xmlns="sap.m" xmlns:l="sap.ui.layout" xmlns:c="sap.ui.commons" xmlns:f="sap.ui.layout.form"
        controllerName="sap.ui.xml.view.View2" xmlns:html="http://www.w3.org/1999/xhtml">
        <Page showNavButton="true"  >
         <content>
            <Label text="Second View" />
         </content>
        </Page>
</core:View>

I am able to get the data from the loaded view, but when I try to reload the view2 the value of loadedView1.getController() is null.


回答1:


Instantiate a global JSONModel in your component and store the view count there. The model can be made globally available by calling setModel in the component's onInit method.



来源:https://stackoverflow.com/questions/30896772/issue-while-getting-data-from-one-xml-view-controller-to-another-view-controller

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