Here’s a simple example on how to setup a simple JSON file as a config file for your directive/module:
What happens here is as follows:
- The directive loads.
- It asks for the config from the service (or it could do it on its own).
$http.get
returns a promise, and theinit
happens only after we get the data.- In this example, the config holds only our Bing map API key. It could also hold urls, delimiters and any other piece of data you would like to use before initializing your directive.
You could expand this code for many other use cases. For instance, setup a default config in case the get
has failed for some reason.
Hi. It’s a good reminder, that angular’s $http.get() returns a promise! By the way, the methods success() and error() are angular’s non standard (callback-like) additions to the promise object. They were deprecated in angular 1.5.0 (https://code.angularjs.org/1.5.0/docs/api/ng/service/$http). So it is probably better to use instead the standard methods then() and catch().