2
我已經定義了一個新的自定義模塊:爲什麼我的格式化程序在控制器中不工作?
狀態,應用程序/ formatter.js
sap.ui.define([], function() {
"use strict";
return {
stringToInt: function(value) {
console.log("Called: " + value);
return parseInt(value);
}
}
});
然後在我控制我的演練Step 23將它定義爲依賴等,並添加控制器屬性。
sap.ui.define([
"sap/ui/core/mvc/Controller",
"status-app/formatter"
], function (Controller, formatter) {
"use strict";
return Controller.extend("stibam-status.Main", {
//formatter: formatter,
formatter: {
stringToInt: function(value) {
console.log(value);
return "";
}
},
[...]
});
我已經嘗試了代碼的兩種變體,但沒有調用格式化程序。我在我的onInit函數this.formatter
中打印,但設置正確。爲什麼只有在視圖中使用匿名函數時,我的視圖纔會調用我的格式化程序?
不工作
oColListItem.addCell((new sap.m.Text()).bindText({
path: "statusData>AnzPdf",
formatter: ".formatter.stringToInt"
}));
工作
oColListItem.addCell((new sap.m.Text()).bindText({
path: "statusData>AnzPdf",
formatter: function(value) {
console.log(value);
return parseInt(value);
}
}));
好的,謝謝,但現在我得到錯誤'this.formatter'未定義。這似乎是我當前的元素(文本元素),而不是控制器。 – Gerrit
爲'JSView.createContent()'內部的代碼添加了第二個示例。當在Controller中時,也許你必須保存這個? 'var that = this;/* ...在回調函數中的某處* /格式化程序:that.formatter.stringToInt' – schnoedel
如果仍然無法使其運行,請發佈更多代碼,特別是在創建控件的上下文中。 – schnoedel