我无法将数据从
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视图中将参数传递给处理函数.
原文链接:https://www.f2er.com/xml/292335.htmlNew 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手动执行此操作,如上所述.但是,还有其他语法可以直接传递事件参数,源代码控制和数据绑定,因此您可能根本不需要该事件.
简短演示:
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>