Reading property file values ​​in Angular

I read several answers about using the service $httpto access the properties file, but now I'm sure how it will correspond to this scenario. I created a service that returns the hostnames from the poperties file, the calling client for this service should make a blocking call to this service and only act when reading the properties file.

var serviceMod = angular.module('serviceModule',[])
.factory('configService', function($http){
    return {
        getValue: function(key){
            $http.get("js/resources/urls.properties").success(function(response){

                console.log('how to send this response to clients sync??? ' + response)

            })
            return ????

        }
    }
})

someOtherControllr.js

var urlValue = configService.getValue('url')

The problem I encountered is related to the nature of the aync service $http. By the time the response is received by the callback, the main thread has already completed by doingsomeOtherController.js

+4
source share
2 answers

, . $http ( return $http.get ). AngularJS $q $http docs ...

.factory('configService', function($http) {
    return {
        getValue: function(key) {
            return $http.get('js/resources/urls.properties');
        }
    }
});

var urlValue;

// --asynchronous
configService.getValue('url').then(function(response) {
    urlValue = response.data; // -- success logic
});

console.log('be mindful - I will execute before you get a response');
[...]
+2

- ( . ):

    getValue: function(key, onSuccess){
        $http.get("js/resources/urls.properties").success(function(response){
            onSuccess(response);
   })
0

Source: https://habr.com/ru/post/1610004/


All Articles