2012-01-20 22:04:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
2012-03-26 20:01:24 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc object
|
2012-06-12 06:49:24 +00:00
|
|
|
* @name ng.$controllerProvider
|
2012-03-26 20:01:24 +00:00
|
|
|
* @description
|
2012-06-12 06:49:24 +00:00
|
|
|
* The {@link ng.$controller $controller service} is used by Angular to create new
|
2012-03-26 20:01:24 +00:00
|
|
|
* controllers.
|
|
|
|
|
*
|
|
|
|
|
* This provider allows controller registration via the
|
2012-06-12 06:49:24 +00:00
|
|
|
* {@link ng.$controllerProvider#register register} method.
|
2012-03-26 20:01:24 +00:00
|
|
|
*/
|
2012-01-20 22:04:53 +00:00
|
|
|
function $ControllerProvider() {
|
2012-06-06 22:54:40 +00:00
|
|
|
var controllers = {},
|
2013-04-30 18:47:23 +00:00
|
|
|
CNTRL_REG = /^(\S+)(\s+as\s+(\w+))?$/;
|
2012-03-26 20:01:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @ngdoc function
|
2012-06-12 06:49:24 +00:00
|
|
|
* @name ng.$controllerProvider#register
|
|
|
|
|
* @methodOf ng.$controllerProvider
|
2012-03-26 20:01:24 +00:00
|
|
|
* @param {string} name Controller name
|
|
|
|
|
* @param {Function|Array} constructor Controller constructor fn (optionally decorated with DI
|
|
|
|
|
* annotations in the array notation).
|
|
|
|
|
*/
|
|
|
|
|
this.register = function(name, constructor) {
|
2012-04-29 05:23:38 +00:00
|
|
|
if (isObject(name)) {
|
|
|
|
|
extend(controllers, name)
|
|
|
|
|
} else {
|
|
|
|
|
controllers[name] = constructor;
|
|
|
|
|
}
|
2012-03-26 20:01:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-01-28 00:18:16 +00:00
|
|
|
this.$get = ['$injector', '$window', function($injector, $window) {
|
2012-01-20 22:04:53 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @ngdoc function
|
2012-06-12 06:49:24 +00:00
|
|
|
* @name ng.$controller
|
2012-01-20 22:04:53 +00:00
|
|
|
* @requires $injector
|
|
|
|
|
*
|
2012-03-26 20:01:24 +00:00
|
|
|
* @param {Function|string} constructor If called with a function then it's considered to be the
|
|
|
|
|
* controller constructor function. Otherwise it's considered to be a string which is used
|
|
|
|
|
* to retrieve the controller constructor using the following steps:
|
|
|
|
|
*
|
|
|
|
|
* * check if a controller with given name is registered via `$controllerProvider`
|
|
|
|
|
* * check if evaluating the string on the current scope returns a constructor
|
|
|
|
|
* * check `window[constructor]` on the global `window` object
|
|
|
|
|
*
|
2012-01-28 00:18:16 +00:00
|
|
|
* @param {Object} locals Injection locals for Controller.
|
2012-01-20 22:04:53 +00:00
|
|
|
* @return {Object} Instance of given controller.
|
|
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* `$controller` service is responsible for instantiating controllers.
|
|
|
|
|
*
|
2013-03-22 09:45:08 +00:00
|
|
|
* It's just a simple call to {@link AUTO.$injector $injector}, but extracted into
|
2012-01-20 22:04:53 +00:00
|
|
|
* a service, so that one can override this service with {@link https://gist.github.com/1649788
|
|
|
|
|
* BC version}.
|
|
|
|
|
*/
|
2012-06-06 22:54:40 +00:00
|
|
|
return function(expression, locals) {
|
|
|
|
|
var instance, match, constructor, identifier;
|
2012-03-26 20:01:24 +00:00
|
|
|
|
2012-06-06 22:54:40 +00:00
|
|
|
if(isString(expression)) {
|
|
|
|
|
match = expression.match(CNTRL_REG),
|
|
|
|
|
constructor = match[1],
|
|
|
|
|
identifier = match[3];
|
|
|
|
|
expression = controllers.hasOwnProperty(constructor)
|
|
|
|
|
? controllers[constructor]
|
|
|
|
|
: getter(locals.$scope, constructor, true) || getter($window, constructor, true);
|
|
|
|
|
|
|
|
|
|
assertArgFn(expression, constructor, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
instance = $injector.instantiate(expression, locals);
|
|
|
|
|
|
|
|
|
|
if (identifier) {
|
2013-05-24 18:00:14 +00:00
|
|
|
if (!(locals && typeof locals.$scope == 'object')) {
|
|
|
|
|
throw ngError(47, "Cannot export controller '{0}' as '{1}'! No $scope object provided via `locals`.", constructor || expression.name, identifier);
|
2012-06-06 22:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
locals.$scope[identifier] = instance;
|
2012-01-28 00:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-06 22:54:40 +00:00
|
|
|
return instance;
|
2012-01-20 22:04:53 +00:00
|
|
|
};
|
|
|
|
|
}];
|
|
|
|
|
}
|