我正在使用 SAP UI5 1.52。我的格式化程序文件是一个单独的文件并加载到控制器中。但登录this格式化程序会返回视图实例而不是控件实例。
我之前曾提到过这个问题,并尝试使用绝对路径并更改了格式化程序中返回对象的方式。它抛出一个错误,指出找不到函数。
<MyControl xmlns:core="sap.ui.core" core:require="{ format: 'demo/model/format' }"
property="{
path: '...',
formatter: 'format'
}"
/><!-- Note: remove the dot (.) in front of the formatter function name -->
Run Code Online (Sandbox Code Playgroud)
sap.ui.define([], function() { // location: "demo/model/format.js"
"use strict";
return function(data) {
// this === control instance
};
});
Run Code Online (Sandbox Code Playgroud)
从 UI5 1.69 开始,我们可以直接在视图定义中 require 模块。直接在绑定信息中要求和分配格式化程序可以让我们用作this相应的控件实例。
文档中也提到了这一点:
sap/ui/base/ManagedObject#bindProperty:
当调用属性绑定(简单或复合)的格式化程序时,托管对象将作为
this上下文给出
格式化程序函数的上下文
this通常设置为拥有绑定的控件(或托管对象)。