Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

SAPUI5 variables scopes

Hi all,

I am new to sapui5 development and followed the tutorials on sapui5 developer guide. I'm not an experienced developer but did some development in the past on iOS (xcode). What still confuses me on the sapui5 development is variable scoping. In xcode I am used to the fact that all variables I declare inside an method or function have a local scope and when I want to use an variable through the whole controller file I have to declare it inside the "viewController" class but outside any function itself.

My question is if there is something like that in SAPUI5 as well? is it possible to define a variable inside a controller file which you can use in all methods defined in that controller file? I know you have to be careful with global variables but sometimes it might still be useful?

Kind Regards,

Nico van der Linden

Former Member

Hi Linden,

You mean to say, you wanted to have a Global Variable in the controller? where you can use the same at multiple places. Yes, there are ways to do that. You can define the variables in onInit life cycle as

this.oGlobalValue = [];

window.oGlobalValue = [];

Above can also be define after the controller function.

But, In general using global variables is not a good practice. Instead, why don't use modeling concept of SAPUI5? Something like below -





  ], function(jQuery, JSONModel, Controller) {

  "use strict";

  var PageController = Controller.extend("demo.FirstPage", {

  onInit: function (oEvent) {

   var oModel = new JSONModel();



  onItemPress: function(oEvent){

   var oValue = oEvent.getParameter("newValue");

   sap.ui.getCore().getModel().setProperty("/oSelectedValue",oValue); //Set the Property Value


  onSearch: function(oEvent){

  var oSearchValue = oEvent.getParameter("searchValue");

  var oSelectedValue = sap.ui.getCore().getModel().getProperty("/oSelectedValue"); //Get the Property Value

  if(oSearchValue === oSelectedValue){

  sap.ui.getCore().getModel().setProperty("/oSelectedValue",oValue); //Overwrite property value (or) set the property value again




  return PageController;


if you check the above sample, oSelectedValue holds a value where I wanted to use in different functions. I can set the property, get the property and overwrite the property as well. Try something like this. If this is not what you are expecting, can you please elaborate the scenario?


Sai Vellanki.

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question