ritcoder ritcoder - 1 year ago 79
Javascript Question

How to check if a directive or controller is available in a module using Angularjs

In angularjs, given a module, how do you check if a directive/controller exists given a module.

I have a module and I want to know if some particular directives have been loaded. Below is some sample code:

var module = angular.module('myModule');
//check if controller exists
if (module.hasController('my.first.controller')){
//do something
if (module.hasDirective('my.first.directive')){
//do something

I have implemented this in a way. Looking for a better way of doing it if it is available by default.

Is this possible?
If so, how do you do this?

Answer Source

Use this code to check if a service exists.


To check if a directive exists, you must add a Directive suffix after the directive name:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download