* @ngdoc provider * @name $controllerProvider * @description * The ng.$controller $controller service is used by Angular to create new * controllers. * * This provider allows controller registration via the * ng.$controllerProvider#register register method.
()
| 7181 | * {@link ng.$controllerProvider#register register} method. |
| 7182 | */ |
| 7183 | function $ControllerProvider() { |
| 7184 | var controllers = {}, |
| 7185 | CNTRL_REG = /^(\S+)(\s+as\s+(\w+))?$/; |
| 7186 | |
| 7187 | |
| 7188 | /** |
| 7189 | * @ngdoc method |
| 7190 | * @name $controllerProvider#register |
| 7191 | * @param {string|Object} name Controller name, or an object map of controllers where the keys are |
| 7192 | * the names and the values are the constructors. |
| 7193 | * @param {Function|Array} constructor Controller constructor fn (optionally decorated with DI |
| 7194 | * annotations in the array notation). |
| 7195 | */ |
| 7196 | this.register = function(name, constructor) { |
| 7197 | assertNotHasOwnProperty(name, 'controller'); |
| 7198 | if (isObject(name)) { |
| 7199 | extend(controllers, name); |
| 7200 | } else { |
| 7201 | controllers[name] = constructor; |
| 7202 | } |
| 7203 | }; |
| 7204 | |
| 7205 | |
| 7206 | this.$get = ['$injector', '$window', function($injector, $window) { |
| 7207 | |
| 7208 | /** |
| 7209 | * @ngdoc service |
| 7210 | * @name $controller |
| 7211 | * @requires $injector |
| 7212 | * |
| 7213 | * @param {Function|string} constructor If called with a function then it's considered to be the |
| 7214 | * controller constructor function. Otherwise it's considered to be a string which is used |
| 7215 | * to retrieve the controller constructor using the following steps: |
| 7216 | * |
| 7217 | * * check if a controller with given name is registered via `$controllerProvider` |
| 7218 | * * check if evaluating the string on the current scope returns a constructor |
| 7219 | * * check `window[constructor]` on the global `window` object |
| 7220 | * |
| 7221 | * @param {Object} locals Injection locals for Controller. |
| 7222 | * @return {Object} Instance of given controller. |
| 7223 | * |
| 7224 | * @description |
| 7225 | * `$controller` service is responsible for instantiating controllers. |
| 7226 | * |
| 7227 | * It's just a simple call to {@link auto.$injector $injector}, but extracted into |
| 7228 | * a service, so that one can override this service with [BC version](https://gist.github.com/1649788). |
| 7229 | */ |
| 7230 | return function(expression, locals) { |
| 7231 | var instance, match, constructor, identifier; |
| 7232 | |
| 7233 | if(isString(expression)) { |
| 7234 | match = expression.match(CNTRL_REG), |
| 7235 | constructor = match[1], |
| 7236 | identifier = match[3]; |
| 7237 | expression = controllers.hasOwnProperty(constructor) |
| 7238 | ? controllers[constructor] |
| 7239 | : getter(locals.$scope, constructor, true) || getter($window, constructor, true); |
| 7240 |
nothing calls this directly
no test coverage detected