* @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.
()
| 7281 | * {@link ng.$controllerProvider#register register} method. |
| 7282 | */ |
| 7283 | function $ControllerProvider() { |
| 7284 | var controllers = {}, |
| 7285 | CNTRL_REG = /^(\S+)(\s+as\s+(\w+))?$/; |
| 7286 | |
| 7287 | |
| 7288 | /** |
| 7289 | * @ngdoc method |
| 7290 | * @name $controllerProvider#register |
| 7291 | * @param {string|Object} name Controller name, or an object map of controllers where the keys are |
| 7292 | * the names and the values are the constructors. |
| 7293 | * @param {Function|Array} constructor Controller constructor fn (optionally decorated with DI |
| 7294 | * annotations in the array notation). |
| 7295 | */ |
| 7296 | this.register = function(name, constructor) { |
| 7297 | assertNotHasOwnProperty(name, 'controller'); |
| 7298 | if (isObject(name)) { |
| 7299 | extend(controllers, name); |
| 7300 | } else { |
| 7301 | controllers[name] = constructor; |
| 7302 | } |
| 7303 | }; |
| 7304 | |
| 7305 | |
| 7306 | this.$get = ['$injector', '$window', function($injector, $window) { |
| 7307 | |
| 7308 | /** |
| 7309 | * @ngdoc service |
| 7310 | * @name $controller |
| 7311 | * @requires $injector |
| 7312 | * |
| 7313 | * @param {Function|string} constructor If called with a function then it's considered to be the |
| 7314 | * controller constructor function. Otherwise it's considered to be a string which is used |
| 7315 | * to retrieve the controller constructor using the following steps: |
| 7316 | * |
| 7317 | * * check if a controller with given name is registered via `$controllerProvider` |
| 7318 | * * check if evaluating the string on the current scope returns a constructor |
| 7319 | * * check `window[constructor]` on the global `window` object |
| 7320 | * |
| 7321 | * @param {Object} locals Injection locals for Controller. |
| 7322 | * @return {Object} Instance of given controller. |
| 7323 | * |
| 7324 | * @description |
| 7325 | * `$controller` service is responsible for instantiating controllers. |
| 7326 | * |
| 7327 | * It's just a simple call to {@link auto.$injector $injector}, but extracted into |
| 7328 | * a service, so that one can override this service with [BC version](https://gist.github.com/1649788). |
| 7329 | */ |
| 7330 | return function(expression, locals) { |
| 7331 | var instance, match, constructor, identifier; |
| 7332 | |
| 7333 | if(isString(expression)) { |
| 7334 | match = expression.match(CNTRL_REG), |
| 7335 | constructor = match[1], |
| 7336 | identifier = match[3]; |
| 7337 | expression = controllers.hasOwnProperty(constructor) |
| 7338 | ? controllers[constructor] |
| 7339 | : getter(locals.$scope, constructor, true) || getter($window, constructor, true); |
| 7340 |
nothing calls this directly
no test coverage detected