参数传递 – 如何在XML视图SAP UI5中将参数传递给事件处理程序

前端之家收集整理的这篇文章主要介绍了参数传递 – 如何在XML视图SAP UI5中将参数传递给事件处理程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法将数据从 XML视图发送到控制器.在JS视图中很容易实现.

例如:-

在JS视图中: –

var btn = new sap.m.Button({
    text:"click",tap:function(){
          callFunction(oEvent,"mycustomString");
    }
});

如何使用XML视图实现相同的功能.

<Button text="click" tap="callFunction"/>

以上只会传递事件而不是“mycustomString”.
我该怎么做?

这是一个老问题,但 as of version 1.56现在支持直接在XML视图中将参数传递给处理函数.

New Event Handler Parameter Syntax

When event handlers are assigned to control events in XML views,you can now also specify parameters which can be passed to the event handler. The parameters can be static values as well as bindings and even expressions. This feature helps to reduce controller code and avoid unnecessary controller methods,and separates the controller logic from the retrieval of the required input values.

所以你现在可以这样做:

<Button text="click" tap=".callFunction($event,'mycustomString')" />

请注意,只要您传递至少一个参数,事件本身将不再自动传递,因此您需要通过传递$event手动执行此操作,如上所述.但是,还有其他语法可以直接传递事件参数,源代码控制和数据绑定,因此您可能根本不需要该事件.

功能的文档可在Demo Kit在线获得.

简短演示:

sap.ui.define([
  "sap/ui/core/mvc/Controller"
],function(Controller) {
  "use strict";

  return Controller.extend("myController",{
    callFunction: function(sButtonText,sVal) {
      alert("Clicked " + sButtonText + " with value " + sVal);
    }
  });
});

sap.ui.xmlview({
  viewContent: $('#myView').html()
}).placeAt('content');
<html>

  <head>
    <Meta charset="utf-8">
    <script id='sap-ui-bootstrap' src='https://sapui5.hana.ondemand.com/resources/sap-ui-core.js' data-sap-ui-libs='sap.m'></script>
    <script id="myView" type="sapui5/xmlview">
      <mvc:View controllerName="myController" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m">
        <Button text="Button 1" press=".callFunction(${$source>/text},'ABCDEF')" />
        <Button text="Button 2" press=".callFunction(${$source>/text},'UVWXYZ')" />
      </mvc:View>
    </script>
  </head>

  <body class='sapUiBody'><div id='content'></div></body>

</html>

猜你在找的XML相关文章