From 98dc205d9a729740a5cf4b779c8d6bb380eaa98f Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Fri, 15 Jan 2016 11:22:56 +0000 Subject: [PATCH] fix($compile): only bind required controllers if `bindToController` is truthy --- src/ng/compile.js | 4 +-- test/ng/compileSpec.js | 59 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index 865120e50440..bbfb88d9d312 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -280,7 +280,7 @@ * passed to the linking function will also be an object with matching keys, whose values will hold the corresponding * controllers. * - * If the `require` property is an object and the directive provides a controller, then the required controllers are + * If the `require` property is an object and `bindToController` is truthy, then the required controllers are * bound to the controller using the keys of the `require` property. This binding occurs after all the controllers * have been constructed but before `$onInit` is called. * See the {@link $compileProvider#component} helper for an example of how this can be used. @@ -2497,7 +2497,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { // Bind the required controllers to the controller, if `require` is an object forEach(controllerDirectives, function(controllerDirective, name) { var require = controllerDirective.require; - if (!isArray(require) && isObject(require)) { + if (controllerDirective.bindToController && !isArray(require) && isObject(require)) { extend(elementControllers[name].instance, getControllers(name, require, $element, elementControllers)); } }); diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 7353471ba43d..afaea8fead5c 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -5338,7 +5338,7 @@ describe('$compile', function() { }); }); - it('should bind the required controllers to the directive controller, if provided as an object', function() { + it('should bind the required controllers to the directive controller, if provided as an object and bindToController is truthy', function() { var parentController, siblingController; function ParentController() { this.name = 'Parent'; } @@ -5355,8 +5355,11 @@ describe('$compile', function() { return { restrict: 'E', scope: {}, + controllerAs: '$ctrl', require: { container: '^parent', friend: 'sibling' }, - controller: MeController + bindToController: true, + controller: MeController, + controllerAs: '$ctrl' }; }) .directive('parent', function() { @@ -5381,6 +5384,50 @@ describe('$compile', function() { }); }); + + it('should not bind required controllers bindToController is falsy', function() { + var parentController, siblingController; + + function ParentController() { this.name = 'Parent'; } + function SiblingController() { this.name = 'Sibling'; } + function MeController() { this.name = 'Me'; } + MeController.prototype.$onInit = function() { + parentController = this.container; + siblingController = this.friend; + }; + spyOn(MeController.prototype, '$onInit').andCallThrough(); + + angular.module('my', []) + .directive('me', function() { + return { + restrict: 'E', + scope: {}, + require: { container: '^parent', friend: 'sibling' }, + controller: MeController + }; + }) + .directive('parent', function() { + return { + restrict: 'E', + scope: {}, + controller: ParentController + }; + }) + .directive('sibling', function() { + return { + controller: SiblingController + }; + }); + + module('my'); + inject(function($compile, $rootScope, meDirective) { + element = $compile('')($rootScope); + expect(MeController.prototype.$onInit).toHaveBeenCalled(); + expect(parentController).toBeUndefined(); + expect(siblingController).toBeUndefined(); + }); + }); + it('should bind required controllers to controller that has an explicit constructor return value', function() { var parentController, siblingController, meController; @@ -5404,7 +5451,9 @@ describe('$compile', function() { restrict: 'E', scope: {}, require: { container: '^parent', friend: 'sibling' }, - controller: MeController + bindToController: true, + controller: MeController, + controllerAs: '$ctrl' }; }) .directive('parent', function() { @@ -5453,7 +5502,9 @@ describe('$compile', function() { restrict: 'E', scope: {}, require: { container: '^parent', friend: 'sibling' }, - controller: MeController + bindToController: true, + controller: MeController, + controllerAs: '$ctrl' }; }) .directive('parent', function() {