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

Commit cae40ca

Browse files
fix($compile): only bind required controllers if bindToController is truthy
1 parent 4b304a6 commit cae40ca

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

src/ng/compile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@
280280
* passed to the linking function will also be an object with matching keys, whose values will hold the corresponding
281281
* controllers.
282282
*
283-
* If the `require` property is an object and the directive provides a controller, then the required controllers are
283+
* If the `require` property is an object and `bindToController` is truthy, then the required controllers are
284284
* bound to the controller using the keys of the `require` property. This binding occurs after all the controllers
285285
* have been constructed but before `$onInit` is called.
286286
* See the {@link $compileProvider#component} helper for an example of how this can be used.
@@ -2497,7 +2497,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
24972497
// Bind the required controllers to the controller, if `require` is an object
24982498
forEach(controllerDirectives, function(controllerDirective, name) {
24992499
var require = controllerDirective.require;
2500-
if (!isArray(require) && isObject(require)) {
2500+
if (controllerDirective.bindToController && !isArray(require) && isObject(require)) {
25012501
extend(elementControllers[name].instance, getControllers(name, require, $element, elementControllers));
25022502
}
25032503
});

test/ng/compileSpec.js

+54-4
Original file line numberDiff line numberDiff line change
@@ -5338,7 +5338,7 @@ describe('$compile', function() {
53385338
});
53395339
});
53405340

5341-
it('should bind the required controllers to the directive controller, if provided as an object', function() {
5341+
it('should bind the required controllers to the directive controller, if provided as an object and bindToController is truthy', function() {
53425342
var parentController, siblingController;
53435343

53445344
function ParentController() { this.name = 'Parent'; }
@@ -5356,7 +5356,9 @@ describe('$compile', function() {
53565356
restrict: 'E',
53575357
scope: {},
53585358
require: { container: '^parent', friend: 'sibling' },
5359-
controller: MeController
5359+
bindToController: true,
5360+
controller: MeController,
5361+
controllerAs: '$ctrl'
53605362
};
53615363
})
53625364
.directive('parent', function() {
@@ -5381,6 +5383,50 @@ describe('$compile', function() {
53815383
});
53825384
});
53835385

5386+
5387+
it('should not bind required controllers bindToController is falsy', function() {
5388+
var parentController, siblingController;
5389+
5390+
function ParentController() { this.name = 'Parent'; }
5391+
function SiblingController() { this.name = 'Sibling'; }
5392+
function MeController() { this.name = 'Me'; }
5393+
MeController.prototype.$onInit = function() {
5394+
parentController = this.container;
5395+
siblingController = this.friend;
5396+
};
5397+
spyOn(MeController.prototype, '$onInit').andCallThrough();
5398+
5399+
angular.module('my', [])
5400+
.directive('me', function() {
5401+
return {
5402+
restrict: 'E',
5403+
scope: {},
5404+
require: { container: '^parent', friend: 'sibling' },
5405+
controller: MeController
5406+
};
5407+
})
5408+
.directive('parent', function() {
5409+
return {
5410+
restrict: 'E',
5411+
scope: {},
5412+
controller: ParentController
5413+
};
5414+
})
5415+
.directive('sibling', function() {
5416+
return {
5417+
controller: SiblingController
5418+
};
5419+
});
5420+
5421+
module('my');
5422+
inject(function($compile, $rootScope, meDirective) {
5423+
element = $compile('<parent><me sibling></me></parent>')($rootScope);
5424+
expect(MeController.prototype.$onInit).toHaveBeenCalled();
5425+
expect(parentController).toBeUndefined();
5426+
expect(siblingController).toBeUndefined();
5427+
});
5428+
});
5429+
53845430
it('should bind required controllers to controller that has an explicit constructor return value', function() {
53855431
var parentController, siblingController, meController;
53865432

@@ -5404,7 +5450,9 @@ describe('$compile', function() {
54045450
restrict: 'E',
54055451
scope: {},
54065452
require: { container: '^parent', friend: 'sibling' },
5407-
controller: MeController
5453+
bindToController: true,
5454+
controller: MeController,
5455+
controllerAs: '$ctrl'
54085456
};
54095457
})
54105458
.directive('parent', function() {
@@ -5453,7 +5501,9 @@ describe('$compile', function() {
54535501
restrict: 'E',
54545502
scope: {},
54555503
require: { container: '^parent', friend: 'sibling' },
5456-
controller: MeController
5504+
bindToController: true,
5505+
controller: MeController,
5506+
controllerAs: '$ctrl'
54575507
};
54585508
})
54595509
.directive('parent', function() {

0 commit comments

Comments
 (0)