diff --git a/src/auto/injector.js b/src/auto/injector.js index 101c467687e5..5235f7c1e742 100644 --- a/src/auto/injector.js +++ b/src/auto/injector.js @@ -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; diff --git a/src/ng/controller.js b/src/ng/controller.js index 424dc7535840..1b5828fadb41 100644 --- a/src/ng/controller.js +++ b/src/ng/controller.js @@ -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); diff --git a/test/ng/controllerSpec.js b/test/ng/controllerSpec.js index adef4b349982..8627bf88845f 100644 --- a/test/ng/controllerSpec.js +++ b/test/ng/controllerSpec.js @@ -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'; }, diff --git a/test/ng/directive/ngControllerSpec.js b/test/ng/directive/ngControllerSpec.js index bd6d3eee5dfc..89bd23f9c781 100644 --- a/test/ng/directive/ngControllerSpec.js +++ b/test/ng/directive/ngControllerSpec.js @@ -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() { @@ -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('
{{mark}}
')($rootScope); + $rootScope.$digest(); + expect(element.text()).toBe('foo'); + })); it('should publish controller into scope', inject(function($compile, $rootScope) { element = $compile('
{{p.mark}}
')($rootScope);