How to check if an angularjs controller has been defined

后端 未结 5 1615
被撕碎了的回忆
被撕碎了的回忆 2021-01-17 20:35

I\'ve got an app defined this way:

angular.module(\"myApp\", [...])
  .config(function ($stateProvider, $controllerProvider) {
    if (isControllerDefined(co         


        
相关标签:
5条回答
  • 2021-01-17 20:47

    You could use the $controller service and do $controller('myController') and wrap a try-catch arround it so you know if it fails.

    0 讨论(0)
  • 2021-01-17 20:55

    An example of a service that can check if a controller exists. Note that it looks for a global function with specified name as well as a controller in the $controller provider.

    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);
          }
        }
      };
    }]);
    

    See the fiddle for usage.

    0 讨论(0)
  • 2021-01-17 20:58

    There is currently no easy way of fetching a list of controllers. That is hidden for internal use only. You would have to go to the source code and add a public method that return the internal controllers variable (in $ControllerProvider function) https://github.com/angular/angular.js/blob/master/src/ng/controller.js#L16

    this.getControllers = function() {
        return controllers;
        // This will return an object of all the currently defined controllers
      };
    

    Then you can just do

    app.config(function($controllerProvider) {
        var myCtrl = $controllerProvider.getControllers()['myController'];
    });
    
    0 讨论(0)
  • 2021-01-17 21:03

    Since angular 1.5.1 (not released yet at the time of writing), there is a new way to check whether a controller exists or not through the $ControllerProvider.has('MyCtrlName') method.

    Github issue: https://github.com/angular/angular.js/issues/13951

    Github PR: https://github.com/angular/angular.js/pull/14109

    Commit backported in 1.5.1 directly: https://github.com/angular/angular.js/commit/bb9575dbd3428176216355df7b2933d2a72783cd


    Disclaimer: Since many people were interested by this feature, I made a PR, because I also need it is some of my projects. Have fun! :)

    This PR has been based on @trekforever answer, thanks for the hint :)

    0 讨论(0)
  • 2021-01-17 21:13

    I came across this exact same issue the other day. I had a few issues with the currently accepted answer, namely because one of my controllers was performing an initialization call out to the server upon instantiation to populate some data (i.e):

    function ExampleController($scope, ExampleService) {
        ExampleService.getData().then(function(data) {
            $scope.foo = data.foo;
            $scope.bar = data.bar
        });
    }
    

    As it stands, the currently accepted answer will actually instantiate the controller, before discarding it. This lead to multiple API calls being made on each request (one to verify that the controller exists, one to actually use the controller).

    I had a bit of a dig around in the $controller source code and found that there's an undocumented parameter you can pass in called later which will delay instantiation. It will, however, still run all of the checks to ensure that the controller exists, which is perfect!

    angular.factory("util", [ "$controller", function($controller) {
        return {
            controllerExists: function(name) {
                try {
                    // inject '$scope' as a dummy local variable
                    // and flag the $controller with 'later' to delay instantiation
                    $controller(name, { "$scope": {} }, true);
                    return true;
                }
                catch(ex) {
                    return false;
                }
            }
        };
    }]);
    

    UPDATE: Probably a lot easier as a decorator:

    angular.config(['$provide', function($provide) {
        $provide.delegate('$controller', [ '$delegate', function($delegate) {
            $delegate.exists = function(controllerName) {
                try {
                    // inject '$scope' as a dummy local variable
                    // and flag the $controller with 'later' to delay instantiation
                    $delegate(controllerName, { '$scope': {} }, true);
                    return true;
                }
                catch(ex) {
                    return false;
                }
            };
    
            return $delegate;
        }]);
    }]);
    

    Then you can simply inject $controller and call exists(...)

    function($controller) {
        console.log($controller.exists('TestController') ? 'Exists' : 'Does not exist');
    }
    
    0 讨论(0)
提交回复
热议问题