Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs read from properties file

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})        
     });
});
like image 416
Woot4Moo Avatar asked Sep 30 '13 17:09

Woot4Moo


2 Answers

If 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

like image 120
Langdon Avatar answered Nov 20 '22 23:11

Langdon


Simple way is to

  1. 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"

  2. In the html page include this config.js at the top (above the main controller.js): **<script.. src="./scripts/config/config.js"></st>**

  3. In the controller make the following change:

    MainController.js: $scope.appUrl = test1; $scope.appConstant = test2;

like image 26
Barani r Avatar answered Nov 20 '22 22:11

Barani r