3

Have an project that uses Ionic Framework and also $http services. When I'm in development environment need to use the development url api and in production need to use the production url api. I isolated already the base Url, someting like :

.factory('EventsService', function ($http, $q) {
    var baseUrl = 'http://development.api.example.com/'; //development api
   .....

Is it possible to store it in an external file somewhere ( something like event.dev.settings.xml) and have also a similar one event.prod.settings.xml and the build process to use the correct one depending on what kind of build I'm making?

Or is another way ?

sp00m
  • 47,968
  • 31
  • 142
  • 252
  • 1
    For now, I handle it via a [constant](https://docs.angularjs.org/api/auto/service/$provide#constant) that is set depending on whether you're running in a browser/SDK (dev) or in an app (prod), but I'm also interested into a better way to handle it at build time. – sp00m Sep 28 '15 at 15:44
  • Think I found the answer to my question.Thanks for all the help. http://stackoverflow.com/questions/17876439/configuration-file-in-angularjs – Catalin Hatmanu Sep 28 '15 at 23:31

2 Answers2

2

I allways define a RESOURCES object constant where I set some constants like URLs apis, api keys, ... Like this:

.constant('RESOURCES', 
    {
        API_URL: 'https://api.mydomain.com/',
        GCM_ID: '000000000000'        
    }
)

Then, in the factory definition I inject the RESOURCE constant and I use the constants defined in it.

.factory('EventsService', function ($http, $q, RESOURCES) {
    $http.get(RESOURCES.API_URL+'method')........
    //...........
}

This way, if I'm going to use the app in production I only need to modify the RESOURCES constants.

Carlos Mayo
  • 2,054
  • 1
  • 19
  • 35
1

I use a web developing proxy for that. In the proxy I have a mapping to the developer URLs, so there is no need to save different URLs for production and developing in the app. I use Charles.

Joerg
  • 3,102
  • 2
  • 24
  • 30