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

Commit d17fbc3

Browse files
fix(ngController): allow bound constructor fns as controllers
Fixes #10784 Closes #10790
1 parent 0db5b21 commit d17fbc3

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

src/auto/injector.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ function createInjector(modulesToLoad, strictDi) {
829829
// Check if Type is annotated and use just the given function at n-1 as parameter
830830
// e.g. someModule.factory('greeter', ['$window', function(renamed$window) {}]);
831831
// Object creation: http://jsperf.com/create-constructor/2
832-
var instance = Object.create((isArray(Type) ? Type[Type.length - 1] : Type).prototype);
832+
var instance = Object.create((isArray(Type) ? Type[Type.length - 1] : Type).prototype || null);
833833
var returnedValue = invoke(Type, instance, locals, serviceName);
834834

835835
return isObject(returnedValue) || isFunction(returnedValue) ? returnedValue : instance;

src/ng/controller.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function $ControllerProvider() {
111111
// Object creation: http://jsperf.com/create-constructor/2
112112
var controllerPrototype = (isArray(expression) ?
113113
expression[expression.length - 1] : expression).prototype;
114-
instance = Object.create(controllerPrototype);
114+
instance = Object.create(controllerPrototype || null);
115115

116116
if (identifier) {
117117
addIdentifier(locals, identifier, instance, constructor || expression.name);

test/ng/controllerSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ describe('$controller', function() {
2727
expect(ctrl instanceof FooCtrl).toBe(true);
2828
});
2929

30+
it('should allow registration of bound controller functions', function() {
31+
var FooCtrl = function($scope) { $scope.foo = 'bar'; },
32+
scope = {},
33+
ctrl;
34+
35+
var BoundFooCtrl = FooCtrl.bind(null);
36+
37+
$controllerProvider.register('FooCtrl', ['$scope', BoundFooCtrl]);
38+
ctrl = $controller('FooCtrl', {$scope: scope});
39+
40+
expect(scope.foo).toBe('bar');
41+
});
3042

3143
it('should allow registration of map of controllers', function() {
3244
var FooCtrl = function($scope) { $scope.foo = 'foo'; },

test/ng/directive/ngControllerSpec.js

+9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ describe('ngController', function() {
3737
this.mark = 'works';
3838
});
3939

40+
var Foo = function($scope) {
41+
$scope.mark = 'foo';
42+
};
43+
$controllerProvider.register('BoundFoo', ['$scope', Foo.bind(null)]);
4044
}));
4145

4246
afterEach(function() {
@@ -50,6 +54,11 @@ describe('ngController', function() {
5054
expect(element.text()).toBe('Hello Misko!');
5155
}));
5256

57+
it('should instantiate bound constructor functions', inject(function($compile, $rootScope) {
58+
element = $compile('<div ng-controller="BoundFoo">{{mark}}</div>')($rootScope);
59+
$rootScope.$digest();
60+
expect(element.text()).toBe('foo');
61+
}));
5362

5463
it('should publish controller into scope', inject(function($compile, $rootScope) {
5564
element = $compile('<div ng-controller="Public as p">{{p.mark}}</div>')($rootScope);

0 commit comments

Comments
 (0)