Custom configuration in Cordova project

时间:2015-07-28 16:19:12

标签: asp.net angularjs cordova asp.net-web-api

I'm currently working with simple cordova app, integrated with WebAPI server. My question is what approach is best for storing some configuration values in cordova app eg. my WebAPI server address. Now I'm using angular consts, but I can't change them at runtime. I'm looking for something like Web.config in ASP.NET MVC.

1 个答案:

答案 0 :(得分:1)

Angular Constant provider is a singleton object and it can be updated at run time.

Let us say you have a constant provider

app.constant("myConfig", {
        "url": "http://localhost",
        "port": "80"
    });

This can be Injected to the controller and the values can be overridden at run time as below,

app.controller('MainCtrl', function (myConfig) {
        myConfig.url = "example.com"
});

To answer on how to get remote config when angular bootstrapped for the first time,

app.run(function(myConfig, $http){ 
    $http({
            method  : 'GET',
            url     : configURL,
            dataType : 'JSON',
        }).success(function (resp){
            myConfig.url = resp.url;
        });
});

Hope it helps.