0

Currently I am working on a Bluemix app with an Angular frontend. I use an conf.json to declare some settings. I need that because my cústomer wants a white-label application which he can configure via an JSON file

So I need to load the config.json. I use ajax for that. My file to load it is conf.js:

var promise = $.Deferred();
$.getJSON("config.json", function (configjson){
        promise.resolve(configjson);
        return configjson;
    });

I want to be sure, that the conf is loaded, when I start angular, because it uses some stuff out of it.

myApp.js starts Angular:

var myApp = angular.module("myApp",
    ['ui.router','diaNavbar','ngMaterial','dashboard']);

promise.done(function(config){
    console.log("Config: ",config);
    myApp.config(['$stateProvider','$urlRouterProvider','$logProvider','$mdThemingProvider', 
               function($stateProvider,$urlRouterProvider,$logProvider,$mdThemingProvider) {
          // configure Angular with Conf.json
      }]);
    });

So my concept is, that the promise variable (jQuery) indicates, when the config is loaded. When I remove the promise.done function it works, but trows the errors, because config ist not loaded yet. The console.log throws out the config, so that works too. But in combination it doesnt work.

Working Environment: Bluemix, Node.JS Express 4, AngluarJS, JQuery,

I would appreciate any help on this

ralphearle
  • 1,696
  • 13
  • 18
Herget
  • 143
  • 1
  • 10
  • Start by [avoiding the deferred antipattern](http://stackoverflow.com/q/23803743/1048572): `var promise = $.getJSON("config.json");`. – Bergi Feb 17 '16 at 16:24
  • So what does not work in the code you've shown us (not in the error-throwing hypothetical code that does wait to load)? – Bergi Feb 17 '16 at 16:26
  • @Bergi corrected it. Is just does not configure my app. so the screen is white - no error messages. I have included some console.logs oin the myApp.conf function, but it doesnt execute them. So the myApp.config is not working – Herget Feb 17 '16 at 16:44
  • This is all wrong. You would need to manually bootstrap angular in the `getJSON()` callback so the config data is available as soon as it bootstraps – charlietfl Feb 17 '16 at 16:50

0 Answers0