Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit dbffbef

Browse files
committed
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
1 parent 0196411 commit dbffbef

File tree

7 files changed

+74
-8
lines changed

7 files changed

+74
-8
lines changed

angularFiles.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ angularFiles = {
1313
'src/service/browser.js',
1414
'src/service/cacheFactory.js',
1515
'src/service/compiler.js',
16+
'src/service/controller.js',
1617
'src/service/cookieStore.js',
1718
'src/service/cookies.js',
1819
'src/service/defer.js',

src/AngularPublic.js

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ function publishExternalAPI(angular){
7070
$provide.service('$browser', $BrowserProvider);
7171
$provide.service('$cacheFactory', $CacheFactoryProvider);
7272
$provide.service('$compile', $CompileProvider);
73+
$provide.service('$controller', $ControllerProvider);
7374
$provide.service('$cookies', $CookiesProvider);
7475
$provide.service('$cookieStore', $CookieStoreProvider);
7576
$provide.service('$defer', $DeferProvider);

src/directives.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ angularDirective("ng:init", function(expression){
160160
*/
161161
angularDirective("ng:controller", function(expression) {
162162
this.scope(true);
163-
return ['$injector', '$window', function($injector, $window) {
163+
return ['$controller', '$window', function($controller, $window) {
164164
var scope = this,
165165
Controller = getter(scope, expression, true) || getter($window, expression, true);
166166

167167
assertArgFn(Controller, expression);
168-
$injector.instantiate(Controller, {$scope: scope});
168+
$controller(Controller, scope);
169169
}];
170170
});
171171

src/service/controller.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
function $ControllerProvider() {
4+
this.$get = ['$injector', function($injector) {
5+
6+
/**
7+
* @ngdoc function
8+
* @name angular.module.ng.$controller
9+
* @requires $injector
10+
*
11+
* @param {Function} Class Constructor function of a controller to instantiate.
12+
* @param {Object} scope Related scope.
13+
* @return {Object} Instance of given controller.
14+
*
15+
* @description
16+
* `$controller` service is responsible for instantiating controllers.
17+
*
18+
* It's just simple call to {@link angular.module.AUTO.$injector $injector}, but extracted into
19+
* a service, so that one can override this service with {@link https://gist.github.com/1649788
20+
* BC version}.
21+
*/
22+
return function(Class, scope) {
23+
return $injector.instantiate(Class, {$scope: scope});
24+
};
25+
}];
26+
}

src/service/formFactory.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@
102102

103103
function $FormFactoryProvider() {
104104
var $parse;
105-
this.$get = ['$rootScope', '$parse', '$injector',
106-
function($rootScope, $parse_, $injector) {
105+
this.$get = ['$rootScope', '$parse', '$controller',
106+
function($rootScope, $parse_, $controller) {
107107
$parse = $parse_;
108108
/**
109109
* @ngdoc proprety
@@ -136,7 +136,7 @@ function $FormFactoryProvider() {
136136

137137
function formFactory(parent) {
138138
var scope = (parent || formFactory.rootForm).$new();
139-
$injector.instantiate(FormController, {$scope: scope});
139+
$controller(FormController, scope);
140140
return scope;
141141
}
142142

src/service/route.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@
6363
</doc:example>
6464
*/
6565
function $RouteProvider(){
66-
this.$get = ['$rootScope', '$location', '$routeParams', '$injector',
67-
function( $rootScope, $location, $routeParams, $injector) {
66+
this.$get = ['$rootScope', '$location', '$routeParams', '$controller',
67+
function( $rootScope, $location, $routeParams, $controller) {
6868
/**
6969
* @ngdoc event
7070
* @name angular.module.ng.$route#$beforeRouteChange
@@ -280,7 +280,7 @@ function $RouteProvider(){
280280
copy(next.params, $routeParams);
281281
next.scope = parentScope.$new();
282282
if (next.controller) {
283-
$injector.instantiate(next.controller, {$scope: next.scope});
283+
$controller(next.controller, next.scope);
284284
}
285285
}
286286
}

test/service/controllerSpec.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
describe('$controller', function() {
4+
var $controller;
5+
6+
beforeEach(inject(function($injector) {
7+
$controller = $injector.get('$controller');
8+
}));
9+
10+
it('should return instance of given controller class', function() {
11+
var MyClass = function() {},
12+
ctrl = $controller(MyClass);
13+
14+
expect(ctrl).toBeDefined();
15+
expect(ctrl instanceof MyClass).toBe(true);
16+
});
17+
18+
it('should inject arguments', inject(function($http) {
19+
var MyClass = function($http) {
20+
this.$http = $http;
21+
};
22+
23+
var ctrl = $controller(MyClass);
24+
expect(ctrl.$http).toBe($http);
25+
}));
26+
27+
28+
it('should inject given scope', function() {
29+
var MyClass = function($scope) {
30+
this.$scope = $scope;
31+
};
32+
33+
var scope = {},
34+
ctrl = $controller(MyClass, scope);
35+
36+
expect(ctrl.$scope).toBe(scope);
37+
});
38+
});

0 commit comments

Comments
 (0)