在 UI5 中访问控制器中的视图元素 [英] Accessing view elements in controller in UI5

查看:36
本文介绍了在 UI5 中访问控制器中的视图元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的按钮被按下时,我想进入我的控制器,访问我的按钮并将文本更改为 new text 例如.

When my button is pressed, I want to get in my controller, access my button and change the text to new text for example.

onClick 事件工作正常.但我坚持从控制器访问视图元素 (myButton).我知道有关于这个主题的线程,但我无法应用它们中的任何一个.sap.ui.getCore().byId() 例如由于某种原因不起作用.

The onClick event works fine. But I stuck at accessing the view element (myButton) from the controller. I know there are threads about this topic, but I couldn't apply any of them. sap.ui.getCore().byId() for example didn't work for some reason.

有没有人得到一些关于从控制器访问视图元素的提示或详细信息?

Does anyone got some hints or detailed information about accessing view elements from the controller?

推荐答案

应使用 View.createId(sId).createId(sId) 在给定的 sId 前面加上视图的 ID 以创建全局 ID.有了这个,您的 sId 只需要在您的视图中是唯一的.所以你可以使用像button1"这样的东西而没有任何副作用.createId("button1") 将返回类似于 __xmlview1--button1 的内容.如果您使用 XMLViews id 属性将自动转换为全局ID (View.createId() 在内部是 由 xml 解析器调用).

The IDs of the elements of a view shall be created with the View.createId(sId). createId(sId) prefixes the given sId with the ID of the view to create a global ID. With that your sId does only have to be unique inside your view. So you can use something like "button1" without any side effects. createId("button1") will return something like __xmlview1--button1. If you are using XMLViews the id attributes will automatically be converted to global IDs (View.createId() is internally called by the xml parser).

的挂件View.createId(sId)View.byId(sId).它查找具有给定视图区域 ID sId 的元素.作为快捷方式,控制器带来了自己的 byId(sId) 函数 委托给视图 byId() 函数.

The pendant to View.createId(sId) is View.byId(sId). It looks up the the element with the given view-locale ID sId. As a shortcut the Controller brings its own byId(sId) function that delegates to the views byId() function.

<mvc:View xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m" controllerName="view1">
  <Button id="button1" press="onButton1Press" text="show my ID"/>
  <Label id="label1" />
</mvc:View>

onButton1Press:function(oEvent){
  var button = oEvent.getSource();
  var label1 = this.byId("label1");
  label1.setText(button.getId()); //Label displays "__xmlview0--button1"
}

jsbin 的示例.

如果您出于某种原因拥有全局 ID,则必须使用 sap.ui.getCore().byId(sId) 来查找元素.

If you have - for some reason - a global ID then you have to use sap.ui.getCore().byId(sId) to look up the element.

如果您根本不使用视图或不使用 createId() 的 JSViews,那么您的 ID 将是全局的.

If you use no views at all or JSViews without createId(), then your IDs will be global.

这篇关于在 UI5 中访问控制器中的视图元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆