[SOLVED] How to check if a directive or controller is available in a module using Angularjs- Angular.js Recipes

  • 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:

    I have implemented this in a way.

  • Looking for a better way of doing it if it is available by default.

Use this code to check if a service exists.

$injector.has(‘myServiceName’)

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

$injector.has(‘myDirectiveNameDirective’)


@angular_recipe: How to check if a directive or controller is available in a module… #Angular #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:

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?

Use this code to check if a service exists.

suffix after the directive name:

var controllers = []; _.each(app._invokeQueue, function(value, index) { if (value[0] !== ‘$controllerProvider’) { return; } controllers.push(value[2][0]); }); if (controllers.indexOf(‘controller-i-want’) === – 1) { // controller is undefined }

I found some working code here

angular.service(‘ControllerChecker’, [‘$controller’, function($controller) { return { exists: function(controllerName) { if(typeof window[controllerName] == ‘function’) { return true; } try { $controller(controllerName); return true; } catch (error) { return !(error instanceof TypeError); } } }; }]);

Solved the problem by writing a wrapper function that is called to load the controllers and stuff and at such I’m able to tell when each directive is loaded.

This recipe can be found in it’s original form on Stack Over Flow.

Angular.js Recipes are structured in a Cookbook format featuring recipes that contain problem statements and solutions. A detailed explanation follows each problem statement of the recipe. This is usually contained within the solution; however, an optional discussion section can often contain other useful information helping to demonstrate how the solution works.

Angular.js is a JavaScript-based open-source front-end web application framework mainly maintained by Google and by a community of individuals and corporations to address many of the challenges encountered in developing single-page applications. The JavaScript components complement Apache Cordova, the framework used for developing cross-platform mobile apps. It aims to simplify both the development and the testing of such applications by providing a framework for client-side model–view–controller (MVC) and model–view–viewmodel (MVVM) architectures, along with components commonly used in rich Internet applications.

Lost? Begin by working through Angular’s Getting Started Guide to get yourself up-and-running.

[SOLVED] How to check if a directive or controller is available in a module using Angularjs- Angular.js Recipes

You might also like More from author

Comments are closed, but trackbacks and pingbacks are open.