如何在AngularJS中读取Java属性文件? [英] How to read Java property file in AngularJS?

查看:178
本文介绍了如何在AngularJS中读取Java属性文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以从位于Web服务器外部的angularjs读取属性文件吗?

就像在Java中一样,属性文件是从项目中部署的,但是我们可以通过以下方式将项目中的这些文件读取为filter.properties:angularJS中存在任何解决方案.

我试图这样,但是变得不确定.

filter.properties:

  key1 = value1key2 = value2 

sampleController.js

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

解决方案

有多种方法可以访问angularjs中的属性文件.

就像每个文件的属性文件一样,它是一个带有.properties扩展名的文件.由于Java属性文件是键值对,所以在一行中用 = 分隔.

因此,我们可以通过以下方式将属性文件转换为javascript对象:遍历属性文件中的每一行,并用 = 符号将其拆分,并将其存储为javascript对象,这将有助于快速访问它./p>

这是它的javascript实现

 函数extractProperties(propertiesFileContents){var keyValuePairs = propertiesFileContents.split("\ n");属性= {}对于(i = 0; i< keyValuePairs.length; i ++){var keyValueArr = keyValuePairs [i] .trim().split("=");var key = keyValueArr [0];var value = keyValueArr [1];属性[键] =值}返回属性;} 

根据此处的代码,我在此添加 plunker 希望对您有所帮助

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

解决方案

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

这篇关于如何在AngularJS中读取Java属性文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆