Himmators
Himmators

Reputation: 15016

Inject depedency if defined?

I have an angular module called gMap. It outputs google maps. I also have module called Current Position. It gets the current location from the browser location api and broadcasts it when/if the user approves the location.

I would like to allow my gMap module to listen for the broadcast of currentLocation, but I would not like currentLocation to be a requirement in using gMap. If I do this:

angular.module('gMap', ['currentLocation']).

Current location must be defined. Is it possible to broadcast to a module without injecting or use something like "isset" to inject dependencies?

Upvotes: 0

Views: 23

Answers (1)

Mike Pugh
Mike Pugh

Reputation: 6787

You can use the $injector service to see if a service is available.

For example, in some of your gMap's startup code, you can do:

 if( $injector.has('currentLocation') ) {
      // setup your broadcast listener
 }

See $injector docs

Upvotes: 1

Related Questions