简体   繁体   English

如何在AngularJS中读取Java属性文件?

[英]How to read Java property file in AngularJS?

Is there any way to read a properties file from angularjs which resides outside the web server? 有没有办法从位于Web服务器外部的angularjs读取属性文件?

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. 就像在java中部署的属性文件一样,但我们可以在我们的项目中将这些文件作为filter.properties读取,这样任何解决方案都在angularJS中。

I tried like this but getting undefined. 我试过这样但是未定义。

filter.properties: filter.properties:

key1=value1 
key2=value2

sampleController.js 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));
    });
});

There are several ways to access properties files in angularjs. 有几种方法可以访问angularjs中的属性文件。

Like every files properties file is a file with .properties extension. 与每个文件属性文件一样,文件扩展名为.properties。 Since java properties files are key value pair separated by = in a single line. 由于java属性文件是在一行中用=分隔的键值对。

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. 因此,我们可以通过迭代属性文件中的每一行并将其与=符号分开并将其存储为javascript对象来将属性文件转换为javascript对象,这将有助于快速访问它。

Here is its javascript implementation 这是它的javascript实现

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 根据你的代码,我在这里添加一个plunker希望这可以帮助你

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 commented out, and sometimes with white spaces around the = sign. Samuel J Mathew的解决方案适用于我,但我必须处理的属性文件在文件中有多个空行,以及注释掉的行,有时在=符号周围有空格。 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 complex 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;
  }

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

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