diff options
| -rw-r--r-- | angularFiles.js | 1 | ||||
| -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 | ||||
| -rw-r--r-- | test/service/controllerSpec.js | 38 |
7 files changed, 74 insertions, 8 deletions
diff --git a/angularFiles.js b/angularFiles.js index e2a37bb5..05f93bf8 100644 --- a/angularFiles.js +++ b/angularFiles.js @@ -13,6 +13,7 @@ angularFiles = { 'src/service/browser.js', 'src/service/cacheFactory.js', 'src/service/compiler.js', + 'src/service/controller.js', 'src/service/cookieStore.js', 'src/service/cookies.js', 'src/service/defer.js', 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); } } } diff --git a/test/service/controllerSpec.js b/test/service/controllerSpec.js new file mode 100644 index 00000000..8b12eceb --- /dev/null +++ b/test/service/controllerSpec.js @@ -0,0 +1,38 @@ +'use strict'; + +describe('$controller', function() { + var $controller; + + beforeEach(inject(function($injector) { + $controller = $injector.get('$controller'); + })); + + it('should return instance of given controller class', function() { + var MyClass = function() {}, + ctrl = $controller(MyClass); + + expect(ctrl).toBeDefined(); + expect(ctrl instanceof MyClass).toBe(true); + }); + + it('should inject arguments', inject(function($http) { + var MyClass = function($http) { + this.$http = $http; + }; + + var ctrl = $controller(MyClass); + expect(ctrl.$http).toBe($http); + })); + + + it('should inject given scope', function() { + var MyClass = function($scope) { + this.$scope = $scope; + }; + + var scope = {}, + ctrl = $controller(MyClass, scope); + + expect(ctrl.$scope).toBe(scope); + }); +}); |
