最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reading properties file value in Angular - Stack Overflow

programmeradmin1浏览0评论

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);
   })
发布评论

评论列表(0)

  1. 暂无评论