最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to read Java property file in AngularJS? - Stack Overflow

programmeradmin1浏览0评论

Is there any way to read a properties file from angularjs which resides outside the web server?

like in java the property file deployed out of the project but we can read those file in our project as filter.properties in this way any solution is there in angularJS.

I tried like this but getting undefined.

filter.properties:

key1=value1 
key2=value2

sampleController.js

var app = angular.module('sampleApp', []);
    app.controller('sampleController', function($scope, $http) {
    $http.get('filter.properties').then(function (response) {
        console.log('a is ', JSON.stringify(response.data.key1));
    });
});

Is there any way to read a properties file from angularjs which resides outside the web server?

like in java the property file deployed out of the project but we can read those file in our project as filter.properties in this way any solution is there in angularJS.

I tried like this but getting undefined.

filter.properties:

key1=value1 
key2=value2

sampleController.js

var app = angular.module('sampleApp', []);
    app.controller('sampleController', function($scope, $http) {
    $http.get('filter.properties').then(function (response) {
        console.log('a is ', JSON.stringify(response.data.key1));
    });
});
Share Improve this question edited Mar 22, 2017 at 9:28 DimaSan 12.7k15 gold badges71 silver badges80 bronze badges asked Mar 22, 2017 at 7:57 Shiva Goud AShiva Goud A 3221 gold badge6 silver badges18 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

There are several ways to access properties files in angularjs.

Like every files properties file is a file with .properties extension. Since java properties files are key value pair separated by = in a single line.

So we can convert a properties file into javascript object by iterating each lines in properties file and split-ing it with = symbol and storing it as javascript object which will help to access it quickly.

Here is its javascript implementation

function extractProperties(propertiesFileContents){
  var keyValuePairs =propertiesFileContents.split("\n");
  properties ={}
  for (i = 0; i < keyValuePairs.length; i++) {
     var keyValueArr=keyValuePairs[i].trim().split("=");
     var key=keyValueArr[0];
     var value=keyValueArr[1];
     properties[key]=value
  }
  return properties;
}

Based on your code here iam adding a plunker here hope this may help you

Samuel J Mathew's solution works for me, but the properties file I have to deal with have multiple empty lines in the file, together with lines mented out, and sometimes with white spaces around the = sign. So I have to add some checks to handle these situations. The modified result is such, hopefully will be useful to those working with a more plex properties file:

function extractProperties(data){
    const keyValuePairs = data.split("\n");
    properties = {}

    for (var i = 0; i < keyValuePairs.length; i++) {
      const keyValuePair = keyValuePairs[i].trim();
      if (!keyValuePair || keyValuePair[0] === '#') {
        continue;
      }
      const keyValueArr = keyValuePair.split("=");
      const key = keyValueArr[0].trim();
      const value = keyValueArr[1].trim();
      properties[key] = value
    }

    return properties;
  }
发布评论

评论列表(0)

  1. 暂无评论