EDITED
It looks like you are trying to configure a service with options. To load the external configuration file asynchronously, you will have to download the angular application yourself inside the full data load callback, instead of using automatic deployment.
Consider this example to define a service that does not actually have a service URL (it will be something like contact-service.js ):
angular.module('myApp').provider('contactsService', function () { var options = { svcUrl: null, apiKey: null, }; this.config = function (opt) { angular.extend(options, opt); }; this.$get = ['$http', function ($http) { if(!options.svcUrl || !options.apiKey) { throw new Error('Service URL and API Key must be configured.'); } function onContactsLoadComplete(data) { svc.contacts = data.contacts; svc.isAdmin = data.isAdmin || false; } var svc = { isAdmin: false, contacts: null, loadData: function () { return $http.get(options.svcUrl).success(onContactsLoadComplete); } }; return svc; }]; });
Then, when the document is ready, you must make a call to load your configuration file (in this case, using jQuery). In the callback, you then executed your angular app.config using the loaded json data. After running .config, you manually download the application manually. Very important: do not use the ng-app directive if you use this method, or angular will load itself . See this url for details:
http://docs.angularjs.org/guide/bootstrap
Same:
angular.element(document).ready(function () { $.get('/js/config/myconfig.json', function (data) { angular.module('myApp').config(['contactsServiceProvider', function (contactsServiceProvider) { contactsServiceProvider.config({ svcUrl: data.svcUrl, apiKey: data.apiKey }); }]); angular.bootstrap(document, ['myApp']); }); });
UPDATE: Here is a JSFiddle example: http://jsfiddle.net/e8tEX/
Keith Morris Apr 03 '14 at 1:07 on 2014-04-03 01:07
source share