Using external JSON in directive init

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:

  1. The directive loads.
  2. It asks for the config from the service (or it could do it on its own).
  3. $http.get returns a promise, and the init happens only after we get the data.
  4. 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.

One thought on “Using external JSON in directive init

Leave a Reply