简体   繁体   中英

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

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

Now in my JS file, I am initalising my angular app like below:

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? 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

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; })());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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