How to check if an angularjs controller has been defined

后端 未结 5 1616
被撕碎了的回忆
被撕碎了的回忆 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 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 :)

提交回复
热议问题