问题描述
我正在尝试将UI5应用程序连接到OData服务.
I am trying to connect my UI5 Application to a OData-Service.
对于绑定,我使用数据绑定("odataModel"是我的OData服务):
For the binding i use databinding (The 'odataModel' is my OData-Service):
<Text text="{odataModel>/UserSet('MyUsername')/UserName}"/>
现在我想替换XML中的'MyUsername'字符串,因此我正在使用另一种模型.所以我尝试了以下方法:
Now i want to replace the 'MyUsername' String within the XML, therefore i am using another model. So i tried the following:
<Text text="{odataModel>/UserSet('${userModel>/user}')/UserName}"/>
如何将变量放入绑定中?
How do i fit a variable into my binding?
问候
推荐答案
您需要执行元素绑定,并从控制器中为所需的控件或容器设置上下文.只需在相对于"/UserSet('MyUsername')"节点的视图相对对象中设置绑定(在开始时就没有斜杠),并为要进行ElementBinding的控件或容器提供ID.例如:
You need to do Element Binding, and set the context from the controller for the control or container you want.Just set your binding in the view relative (no slash at the begining) to the "/UserSet('MyUsername')" node and give an ID to the control or container where you want to do ElementBinding.For example:
<Text id="myText" text="{odataModel>UserName}"/>
然后从控制器中,只要您具有用户名字符串,就可以执行ElementBinding,例如:
Then from the controller, you can do ElementBinding whenever you have the username string, for example:
onInit(){
var sUsername = this.getView().getModel("userModel").getProperty("user"); // This is your username coming from your "userModel"
var sModelNodeAbsolutePath = "odataModel>/UserSet("+ sUsername +")"; // This is the absolute path to the node for this user, the path in the view will be relative to this node
this.getView().byId("myText").bindElement(sModelNodeAbsolutePath);
}
这篇关于SAPUI5动态数据绑定(OData服务的密钥)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!