简体   繁体   English

如何将字符串从SAPUI5中的html视图传递到控制器方法?

[英]How to pass a string to controller method from html view in SAPUI5?

I need to build a navigation with a dynamic route parameter from controller. 我需要使用来自控制器的动态路线参数来构建导航。

//user.controller.js

(function () {
  sap.ui.controller("app.User", {
    getNavUrl: function (tab) {
      return '#/users/+ this.userId + '/' + tab;
    }
  });
}());

//user.view.html
<div data-sap-ui-type="ui5strap.Nav" data-selection-mode="Single">
  <div data-sap-ui-type="ui5strap.ListNavItem" 
       data-selected="true"
       data-text="{i18n>Grants}" 
       data-href={getNavUrl('grants')}> <!-- cant make this to work! -->
  </div>

  <div data-sap-ui-type="ui5strap.ListNavItem"
       data-text="{i18n>Opportunities}" 
       data-href={getNavUrl('opportunities')}> <!-- cant make this to work! -->
  </div>

</div>

So, the way i'd do this in any MVC - is by passing a string to controller method from within view, but i can't seem to find a way to invoke controller method from view in sapui5. 因此,我将在任何MVC中执行此操作的方式-是通过在视图内将字符串传递给控制器​​方法,但是我似乎找不到在sapui5中从视图调用控制器方法的方法。

You can invoke a method like this: 您可以调用如下方法:

sap.ui.getCore().byId("idofviewhere").oController.onBeforeRendering();

In your case: 在您的情况下:

sap.ui.getCore().byId("idofviewhere").oController.getNavUrl(tab);

Did you try using SAPUI5's internal function handlers? 您是否尝试过使用SAPUI5的内部函数处理程序? Here is an example with a button press: 这是按下按钮的示例:

//user.controller.js

 (function () {
      sap.ui.controller("app.User", {


        navigateFoo: function (oEvent) {
             alert("you are now navigating. If you want to dynamically alter your destination, 
                    consider putting custom data into your UI object, and using 'oEvent.getSource()'
                    in the controller to get access to your UI object, and all custom data inside");
            }
      });
    }());

//user.view.html

<div data-sap-ui-type="sap.ui.commons.Button" id="myButton" data-text="Navigate" data-press="navigateFoo"></div>

In your case, you could check if your listnav items have a 'data-press' or equivalent you can use. 对于您的情况,您可以检查listnav项目是否具有“ data-press”或可以使用的等效按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM