How to check if an angularjs controller has been defined

后端 未结 5 1654
被撕碎了的回忆
被撕碎了的回忆 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: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'];
    });
    

提交回复
热议问题