diff --git a/src/ng/compile.js b/src/ng/compile.js index 129be31f0b88..2eb47a6fc5dc 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1997,9 +1997,12 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { for (i in elementControllers) { controller = elementControllers[i]; var controllerResult = controller(); + if (controllerResult !== controller.instance) { + // If the controller constructor has a return value, overwrite the instance + // from setupControllers and update the element data controller.instance = controllerResult; - $element.data('$' + directive.name + 'Controller', controllerResult); + $element.data('$' + i + 'Controller', controllerResult); if (controller === controllerForBindings) { // Remove and re-install bindToController bindings thisLinkFn.$$destroyBindings(); diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 8bd74c7353d7..d7c52d02bae5 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -4573,6 +4573,41 @@ describe('$compile', function() { }); + it('should correctly assign controller return values for multiple directives', function() { + var directiveController, otherDirectiveController; + module(function() { + + directive('myDirective', function(log) { + return { + scope: true, + controller: function($scope) { + return directiveController = { + foo: 'bar' + }; + } + }; + }); + + directive('myOtherDirective', function(log) { + return { + controller: function($scope) { + return otherDirectiveController = { + baz: 'luh' + }; + } + }; + }); + + }); + + inject(function(log, $compile, $rootScope) { + element = $compile('')($rootScope); + expect(element.data('$myDirectiveController')).toBe(directiveController); + expect(element.data('$myOtherDirectiveController')).toBe(otherDirectiveController); + }); + }); + + it('should get required parent controller', function() { module(function() { directive('nested', function(log) {