[TO DISCUSS] Provide a method to get a list of all defined controllers #13951
Description
Problem
There is no way to detect whether a controller has been defined or not without instanciate it, which isn't a good solution. See discussion on SO: https://stackoverflow.com/questions/19734565/how-to-check-if-an-angularjs-controller-has-been-defined
Use case
Say that we have an angular app which is a generic product which also deals with specific content based on a customer. Say that we have generic controllers that are use by default, except when a specific controller with the customer suffix exists and we want to use that controller, but only if it exists. Otherwise we would use the generic controller. (Basically a controller fallback)
In this case we need to check if the specific controller exists and if it doesn't then use the generic one.
Example
angular.module('app').config ($stateProvider, ClientServiceProvider) ->
_ClientService = ClientServiceProvider.$get()
$stateProvider
.state 'app',
controller: _ClientService.resolveClientCtrl 'AppCtrl'
The resolveClientCtrl
method would check whether or not a specific controller exists or use the provided controller. (this is an example of use, for a specific need)
Proposition 1
Angular already has a list of controllers used internally which aren't shared out of its scope. What we could do is simply add a method that provides the list of defined controller as an array of strings.
A solution close to this has been proposed here on SO: http://stackoverflow.com/a/19736852/2391795
Proposition 2
We could also just add a method which doesn't provide the list of controllers but check whether a controller exists or not.
This has been asked two years ago and has more than 5k views in one SO question. I believe this should be in angular "core".
If one of those propositions is accepted (or both) then I'll make a PR.