diff options
| author | Vojta Jina | 2012-01-20 14:04:53 -0800 |
|---|---|---|
| committer | Vojta Jina | 2012-01-23 13:11:12 -0800 |
| commit | dbffbefb7cd7af2ac063c95378a035aa9fbbd2ff (patch) | |
| tree | 047f7ae83c0573e1f1c22c44cba7644a1ab464b1 /src | |
| parent | 0196411dbe179afe24f4faa6d6503ff3f69472da (diff) | |
| download | angular.js-dbffbefb7cd7af2ac063c95378a035aa9fbbd2ff.tar.bz2 | |
refactor($controller): Add $controller service for instantiating controllers
So that we can allow user to override this service and use BC hack:
https://gist.github.com/1649788
Diffstat (limited to 'src')
| -rw-r--r-- | src/AngularPublic.js | 1 | ||||
| -rw-r--r-- | src/directives.js | 4 | ||||
| -rw-r--r-- | src/service/controller.js | 26 | ||||
| -rw-r--r-- | src/service/formFactory.js | 6 | ||||
| -rw-r--r-- | src/service/route.js | 6 |
5 files changed, 35 insertions, 8 deletions
diff --git a/src/AngularPublic.js b/src/AngularPublic.js index 3614eb9a..bfc50ef8 100644 --- a/src/AngularPublic.js +++ b/src/AngularPublic.js @@ -70,6 +70,7 @@ function publishExternalAPI(angular){ $provide.service('$browser', $BrowserProvider); $provide.service('$cacheFactory', $CacheFactoryProvider); $provide.service('$compile', $CompileProvider); + $provide.service('$controller', $ControllerProvider); $provide.service('$cookies', $CookiesProvider); $provide.service('$cookieStore', $CookieStoreProvider); $provide.service('$defer', $DeferProvider); diff --git a/src/directives.js b/src/directives.js index 61bb6139..e9b678f8 100644 --- a/src/directives.js +++ b/src/directives.js @@ -160,12 +160,12 @@ angularDirective("ng:init", function(expression){ */ angularDirective("ng:controller", function(expression) { this.scope(true); - return ['$injector', '$window', function($injector, $window) { + return ['$controller', '$window', function($controller, $window) { var scope = this, Controller = getter(scope, expression, true) || getter($window, expression, true); assertArgFn(Controller, expression); - $injector.instantiate(Controller, {$scope: scope}); + $controller(Controller, scope); }]; }); diff --git a/src/service/controller.js b/src/service/controller.js new file mode 100644 index 00000000..22fb3b02 --- /dev/null +++ b/src/service/controller.js @@ -0,0 +1,26 @@ +'use strict'; + +function $ControllerProvider() { + this.$get = ['$injector', function($injector) { + + /** + * @ngdoc function + * @name angular.module.ng.$controller + * @requires $injector + * + * @param {Function} Class Constructor function of a controller to instantiate. + * @param {Object} scope Related scope. + * @return {Object} Instance of given controller. + * + * @description + * `$controller` service is responsible for instantiating controllers. + * + * It's just simple call to {@link angular.module.AUTO.$injector $injector}, but extracted into + * a service, so that one can override this service with {@link https://gist.github.com/1649788 + * BC version}. + */ + return function(Class, scope) { + return $injector.instantiate(Class, {$scope: scope}); + }; + }]; +} diff --git a/src/service/formFactory.js b/src/service/formFactory.js index 5d64131e..69c6d717 100644 --- a/src/service/formFactory.js +++ b/src/service/formFactory.js @@ -102,8 +102,8 @@ function $FormFactoryProvider() { var $parse; - this.$get = ['$rootScope', '$parse', '$injector', - function($rootScope, $parse_, $injector) { + this.$get = ['$rootScope', '$parse', '$controller', + function($rootScope, $parse_, $controller) { $parse = $parse_; /** * @ngdoc proprety @@ -136,7 +136,7 @@ function $FormFactoryProvider() { function formFactory(parent) { var scope = (parent || formFactory.rootForm).$new(); - $injector.instantiate(FormController, {$scope: scope}); + $controller(FormController, scope); return scope; } diff --git a/src/service/route.js b/src/service/route.js index 04bcfdb6..f8200fd4 100644 --- a/src/service/route.js +++ b/src/service/route.js @@ -63,8 +63,8 @@ </doc:example> */ function $RouteProvider(){ - this.$get = ['$rootScope', '$location', '$routeParams', '$injector', - function( $rootScope, $location, $routeParams, $injector) { + this.$get = ['$rootScope', '$location', '$routeParams', '$controller', + function( $rootScope, $location, $routeParams, $controller) { /** * @ngdoc event * @name angular.module.ng.$route#$beforeRouteChange @@ -280,7 +280,7 @@ function $RouteProvider(){ copy(next.params, $routeParams); next.scope = parentScope.$new(); if (next.controller) { - $injector.instantiate(next.controller, {$scope: next.scope}); + $controller(next.controller, next.scope); } } } |
