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

fix(ngController): allow bound constructor fns as controllers #10790

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/auto/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ function createInjector(modulesToLoad, strictDi) {
// Check if Type is annotated and use just the given function at n-1 as parameter
// e.g. someModule.factory('greeter', ['$window', function(renamed$window) {}]);
// Object creation: http://jsperf.com/create-constructor/2
var instance = Object.create((isArray(Type) ? Type[Type.length - 1] : Type).prototype);
var instance = Object.create((isArray(Type) ? Type[Type.length - 1] : Type).prototype || null);
var returnedValue = invoke(Type, instance, locals, serviceName);

return isObject(returnedValue) || isFunction(returnedValue) ? returnedValue : instance;
Expand Down
2 changes: 1 addition & 1 deletion src/ng/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function $ControllerProvider() {
// Object creation: http://jsperf.com/create-constructor/2
var controllerPrototype = (isArray(expression) ?
expression[expression.length - 1] : expression).prototype;
instance = Object.create(controllerPrototype);
instance = Object.create(controllerPrototype || null);

if (identifier) {
addIdentifier(locals, identifier, instance, constructor || expression.name);
Expand Down
12 changes: 12 additions & 0 deletions test/ng/controllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ describe('$controller', function() {
expect(ctrl instanceof FooCtrl).toBe(true);
});

it('should allow registration of bound controller functions', function() {
var FooCtrl = function($scope) { $scope.foo = 'bar'; },
scope = {},
ctrl;

var BoundFooCtrl = FooCtrl.bind(null);

$controllerProvider.register('FooCtrl', ['$scope', BoundFooCtrl]);
ctrl = $controller('FooCtrl', {$scope: scope});

expect(scope.foo).toBe('bar');
});

it('should allow registration of map of controllers', function() {
var FooCtrl = function($scope) { $scope.foo = 'foo'; },
Expand Down
9 changes: 9 additions & 0 deletions test/ng/directive/ngControllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ describe('ngController', function() {
this.mark = 'works';
});

var Foo = function($scope) {
$scope.mark = 'foo';
};
$controllerProvider.register('BoundFoo', ['$scope', Foo.bind(null)]);
}));

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

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

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