简体   繁体   English

在AngularJS中的app.config中访问OSGi配置数据

[英]Accessing OSGi config data in app.config in AngularJS

I have a jsp file which is getting data from OSGi configuration in AEM, like below 我有一个jsp文件,它从AEM中的OSGi配置获取数据,如下所示

<c:set var="myParam" value="${myConfig.myParam}" scope="request"/>

Now in my JS file, I am initalising my angular app like below: 现在在我的JS文件中,如下所示启动我的角度应用程序:

var app = angular.module('myapp', []);

app.provider("$provider1", [function () {
    // Default configuration
    var config = {
        key1:   'value'
      };
    return {
        configure: function (params) {
            angular.extend(config, params);
        },
        $get: ['$rootScope', function ($rootScope) {
            return {
                config: config
            };
        }]
    };
}]);

app.config(['$authProvider', function($authProvider) {

    $authProvider.configure({
        key1:               'myCustomDataFromJSP'
    })
}]);

How can I retrieve this myCustomDataFromJSP from my JSP file? 如何从我的JSP文件中检索此myCustomDataFromJSP In config phase we can't access scope. 在配置阶段,我们无法访问范围。

Thanks 谢谢

I would do it in next way: 我将以以下方式进行操作:

  1. Add your variable as a data attribute somewhere on your page, like this: 将变量作为数据属性添加到页面上的某处,如下所示:

<div id="config" data-jspvar="${myParam}"> </div>

  1. Now register a constant in your angularjs app 现在在您的angularjs应用程序中注册一个常量

Like this: 像这样:

app.constant('myCustomDataFromJSP', (function() {

  // Define your variable
  var myCustomDataFromJSP = ...; //you can use smth like this 
  //window.document.getElementById('config').dataset.jspvar

  return myCustomDataFromJSP;
})());
  1. Now you can inject this constant into your config block. 现在,您可以将此常量注入到配置块中。

上面的答案是一个很好的答案,但是在DOM中有一个隐藏的输入而不是一个div很好。

<input type='hidden' id="config" data-jspvar="${myParam}"> </input >

app.constant('myCustomDataFromJSP', (function() { var myCustomDataFromJSP = //get the value here return myCustomDataFromJSP; })());

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

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