admin管理员组文章数量:1201592
In angularJS how can I read a value from a properties file?
connection.properties:
url="http://localhost:8080"
user= "me"
get= "GET"
post= "POST"
app.js:
var app = angular.module('testing',[]);
app.controller('testCtrl',function($scope,$http) {
$http({
url: connection.properties.url ,
method: connection.properties.get,
params: {user: connection.properties.user})
});
});
In angularJS how can I read a value from a properties file?
connection.properties:
url="http://localhost:8080"
user= "me"
get= "GET"
post= "POST"
app.js:
var app = angular.module('testing',[]);
app.controller('testCtrl',function($scope,$http) {
$http({
url: connection.properties.url ,
method: connection.properties.get,
params: {user: connection.properties.user})
});
});
Share
Improve this question
asked Sep 30, 2013 at 17:28
Woot4MooWoot4Moo
24.3k18 gold badges98 silver badges156 bronze badges
1
- @patxy connection.properties is a file that lives on my server. – Woot4Moo Commented Sep 30, 2013 at 17:34
3 Answers
Reset to default 14If connection.properties
is a file that lives on your web server, then you simply need to do this:
var app = angular.module('app', []);
app.controller('test', function ($scope, $http) {
$http.get('connection.properties').then(function (response) {
console.log('a is ', response.data.a);
console.log('b is ', response.data.b);
});
});
You can see an example here:
http://plnkr.co/edit/3Ne3roFOwcfVmg2mgnUr?p=preview
Simple way is to
create a js file named
"config.js" (lets assume in the path scripts/config/config.js)
config.js:
var test1="http://testurl.com" var test2="globalconstant"
In the html page include this config.js at the top (above the main controller.js):
**<script.. src="./scripts/config/config.js"></st>**
In the controller make the following change:
MainController.js: $scope.appUrl = test1; $scope.appConstant = test2;
Langdon's answer loads the content of the properties file for me but the value inside the properties are not accessible to me in the format of response.data.a
and response.data.b
etc and always returns undefined
. For the value to be available to me I have to extract the content of the properties file and to turn them into JSON format before I can use it. A modification of the above proposed solution would be such:
var app = angular.module('app', []);
app.controller('test', function ($scope, $http) {
function extractProperties(data){
const lines = data.split("\n");
properties = {}
for (const l of lines) {
const line = l.trim();
if (!line || line[0] === '#') {
continue;
}
const keyValue = line.split("=");
const key = keyValue[0].trim();
const value = keyValue[1].trim();
properties[key] = value
}
return properties;
}
$http.get('connection.properties').then(function (response) {
const properties = extractProperties(response.data);
console.log('URL is ', properties.url);
console.log('User is ', properties.user);
});
});
本文标签: javascriptangularjs read from properties fileStack Overflow
版权声明:本文标题:javascript - angularjs read from properties file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738584921a2101375.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论