简体   繁体   English

将Var从一个javascript类传递到另一个

[英]pass Var from one javascript class to another

I have a controller and a view class.In my controller class I have this code. 我有一个控制器和一个视图类,在我的控制器类中有这个代码。 I am trying to get the AccNo which is passed in the url. 我正在尝试获取在URL中传递的AccNo。 I know this code works fine for retrieving the account number, but I now need to pass the Var in to the view class. 我知道这段代码可以很好地检索帐号,但是现在我需要将Var传递给视图类。

    getURLParameter : function(name) {
        return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
    },

    // Parse URL parameters 
    parseAndSetURLParameters : function(){

        var accNo    = this.getURLParameter('accNo');

    },

First of all you might want to have a look at jQuery.sap.getUriParameters . 首先,您可能想看看jQuery.sap.getUriParameters

To make variables available to a view the easiest and most convenient way is to use a JSONModel like this: 要使变量可用于视图,最简单,最方便的方法是使用JSONModel,如下所示:

parseAndSetURLParameters: function() {
  var oModel = new sap.ui.model.json.JSONModel({
    accNo: jQuery.sap.getUriParameters().get("accNo");
  });
  this.getView().setModel(oModel, "view");
}

and in your XMLView: 并在您的XMLView中:

<Text text="{view>/accNo}" />

Or even more generically you could just pump all uri-parameters in a JSONModel and set it right on your Component (be aware that I use internal api ('mParams') here): 或更一般地说,您可以将所有uri参数泵入JSONModel并将其直接设置在Component上(请注意,我在这里使用内部api('mParams')):

new sap.ui.model.json.JSONModel(jQuery.sap.getUriParameters().mParams);

BR Chris 克里斯·克里斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM