在函数中设置全局变量的值 [英] Set the value of a global variable in a function
问题描述
我目前正在开发 SAP Fiori 应用使用 OData 服务.
I'm currently working on an SAP Fiori app that consumes an OData service.
我在控制器 fonct
中创建了一个方法,用于计算来自我的 OData 的变量.
I created a method in my controller fonct
that calculates a variable coming from my OData.
我想在每次刷新视图时捕获此值并将其放入全局变量中.
I want to capture this value and put it in a global variable every time the view is refreshed.
我创建了一个这样的全局变量:
I created a global variable like this:
var boo1;
return Controller.extend("com.controller.Detail", {...});
并且我在我的 onInit
方法中将 boo1
作为参数传递给了我的方法 fonct
但它是 undefined
.
and I passed boo1
as a parameter in my method fonct
inside my onInit
method but it is undefined
.
这是我的控制器代码片段:
Here's a snippet of my controller's code:
sap.ui.define([
"com/util/Controller"
], function(Controller) {
"use strict";
var boo1;
return Controller.extend("com.controller.Detail", {
onInit: function() {
this.fonct(boo1);
alert(boo1);
},
fonct: function(ovar) {
var that = this;
var oModel = that.getView().getModel();
oModel.read("/alertSet", {
success: function(data) {
var a = JSON.stringify(data);
var b = a.slice(332,-4);
ovar = b;
}
});
}
});
});
推荐答案
您应该在全局范围内(即在 sap.ui.define
之前)声明全局变量.
You should declare the global variable at global scope (i.e., before sap.ui.define
).
这篇关于在函数中设置全局变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!