Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

json file which contains my app_key and app_secret, I have written factory service to make available app_key and app_secret everywhere. but I am not getting in correct format, here is my factory method

.factory('UserService', function($http) {
        var obj ={}; 
        $http.get('config.json').success(function(data) {
            obj.app_key = data.app_key;
            obj.app_secret=data.app_secret;
        })
        console.log(obj);
        return {
            app_key : obj.app_key,
            app_secret : obj.app_secret
        }
    })

Here is my config.json file

{
    "url": "",
    "app_key": "myappkeycode",
    "app_secret": "appsecretcodehere"
}

I want my app key and app secret to be accessed everywhere with userService.app_key and userService.app_secret Whats wrong I am doing dont know I am getting object but in following format

Object {}
app_key:"appkeycoming keyhere",
app_secret:"appsecretcominghere"

Which is not working as you see first object is empty.

share|improve this question
1  
It is asynchronous. Put a console.log(data) inside the success function – Weedoze 15 hours ago
    
data is coming but I cant return in format I want – Sudarshan Kalebere 15 hours ago
    
Have you tried my answer ? – Weedoze 8 hours ago

$http.get is an asynchronous call. Then you are calling the value of app_key and app_secret before being set inside the success function.

You should replace your factory by this

module.factory('userService', function() {
    var fac = {};

    fac.getConfig = $http.get('config.json');

    return fac;
});

And call it like this

userService.getConfig().then(function(resp){
    var obj = {};
    obj.app_key = resp.app_key;
    obj.app_secret = resp.app_secret;
})
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.