Javascript SAPUI5从同一控制器中的另一个函数调用函数
我试图通过在自己的函数中存储一些函数代码来让代码看起来更干净。然而,这并没有奏效。控制台中未记录任何错误 这是我的代码:Javascript SAPUI5从同一控制器中的另一个函数调用函数,javascript,sapui5,function-call,Javascript,Sapui5,Function Call,我试图通过在自己的函数中存储一些函数代码来让代码看起来更干净。然而,这并没有奏效。控制台中未记录任何错误 这是我的代码: onPressScan: function() { sap.ndc.BarcodeScanner.scan( // calling the function at this point works function(mResult) { if (!mResult.cancelled) { // call another
onPressScan: function() {
sap.ndc.BarcodeScanner.scan(
// calling the function at this point works
function(mResult) {
if (!mResult.cancelled) {
// call another function from the same controller
this.handleData(mResult.text);
}
},
function(Error) {
sap.m.MessageBox.error("Scanning failed due to following error: " + Error, {
title: "Error while scanning"
});
}
),
handleData: function(data) {
sap.m.MessageBox.success("Calling function worked: " + data, {
title: "Success"
});
}
出于测试目的,我将handleData函数最小化。我刚才尝试的是调用上面一层的函数(而不是抛出两个函数调用…)。这很有效。但是,我需要调用在代码中可以看到的函数。我怎样才能做到这一点
多谢各位 您的上下文已更改,
上下文中的此
不再是控制器
在正确的位置为该添加一个句柄-例如
var that = this;
然后将代码更改为您希望在控制器之外访问此功能的位置:
that.handleData(mResult.text);
您的代码应更改为以下代码:
onPressScan: function() {
sap.ndc.BarcodeScanner.scan(
var that = this;
function(mResult) {
if (!mResult.cancelled) {
that.handleData(mResult.text);
}
},
function(Error) {
sap.m.MessageBox.error("Scanning failed due to following error: " + Error, {
title: "Error while scanning"
});
}
),
handleData: function(data) {
sap.m.MessageBox.success("Calling function worked: " + data, {
title: "Success"
});
}