2

I am trying to load properties file from angular controller, But I am not getting values instead of values I am getting undefined. For that I am using the below code.

      $http.get('Validation.properties').then(function (response) {
        console.log('TestString is ', response.data.TestString);
        console.log('BooleanValue is ', response.data.BooleanValue);            
      });

And my properties file is like

{ "TestString": "Read value from file", "BooleanValue": false }

output is:

TestString is  undefined
BooleanValue is  undefined
4
  • 1
    what display console.log('TestString is ', response.data); Commented Mar 12, 2017 at 8:01
  • 1
    Hit F12 (or Cmd-Alt-I) on Mac. Go to the network panel. Look at the HTTP request for this "properties" file (which is in fact a JSON file). Check that the response is what you expect it to be. Fix the path/content of the file. Commented Mar 12, 2017 at 8:12
  • Can I Use .js file instead of properties file Commented Mar 12, 2017 at 8:57
  • Why do you absolutely want to use a file extension that is not appropriate for what the file contains? Your file is not a properties file. It's also not a JavaScript file. It's a file that contains JSON. The conventional extension for JSON files is .json. It doesn't matter much, and probably won't change anything, but using the wrong extension just makes things confusing. You wouldn't name all your text file with .xls, all your Excel file with .xml, and all your xml files with .doc, would you? Commented Mar 12, 2017 at 9:02

2 Answers 2

1

AS @lin mentioned you must use a json file. But if your properties are constants (means config properties like api_url or anything) you can use a config.js file without any $http request:

angular.module('starter')

.constant('API', {
  url: 'http://example.com/api'
});

And in your controller:

angular.module('starter');

.controller('dashboard', function($scope, API){

    alert(API.url);

})
Sign up to request clarification or add additional context in comments.

Comments

0

Also this answer is not cared of you. You did not answer the users questions in the comments. So, how should this guys help you while you dont answer their questions? Instead of answering the user question you started a new question in your comments: Can I Use .js file instead of properties file. This doesnt make sense at all.

Well, I made this work with an .json file. Please take a look at this demo plnkr.

AngularJS application

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function ($scope, $http) {
  $http.get('./validation-properties.json').then(function (response) {
      console.log('TestString is ', response.data.TestString);
      console.log('BooleanValue is ', response.data.BooleanValue); 
  });
}); 

validation-properties.json file

{ 
  "TestString": "Read value from file", 
  "BooleanValue": false
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.