javascript - Reading properties file value in Angular - Stack Overflow

I have gone through a few replies about using $http service for accessing the properties file, but now

I have gone through a few replies about using $http service for accessing the properties file, but now sure how it would fit in this scenario I have created a service that returns the hostnames from the poperties file, the calling client to this service should make a blocking call to the service and proceed only if the property file is read.

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 am facing is to do with the aync nature of the $http service. By the time the response is received by the callback, the main thread is already finished executing the someOtherController.js

I have gone through a few replies about using $http service for accessing the properties file, but now sure how it would fit in this scenario I have created a service that returns the hostnames from the poperties file, the calling client to this service should make a blocking call to the service and proceed only if the property file is read.

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 am facing is to do with the aync nature of the $http service. By the time the response is received by the callback, the main thread is already finished executing the someOtherController.js

Share Improve this question edited Oct 2, 2015 at 16:51 scniro 17k8 gold badges66 silver badges107 bronze badges asked Oct 2, 2015 at 16:05 tintintintin 5,88716 gold badges74 silver badges104 bronze badges 1
  • was my suggestion able to help you in any way? Please share your findings – scniro Commented Oct 23, 2015 at 22:41
Add a ment  | 

2 Answers 2

Reset to default 2

You need to resolve the promise returned by the service. We can just return the $http call and resolve it in our controller (since return $http.get be a promise itself). Check out the AngularJS $q and $http docs for a bettering understanding of the underlying mechanics going on, and observe the following change...

.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');
[...]

Simple way - use callback (it will still be async. In fact you cant make it sync) :

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745495212a4630161.html

相关推荐

  • javascript - Reading properties file value in Angular - Stack Overflow

    I have gone through a few replies about using $http service for accessing the properties file, but now

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信