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

Commit 98dc205

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

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-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

+55-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'; }
@@ -5355,8 +5355,11 @@ describe('$compile', function() {
53555355
return {
53565356
restrict: 'E',
53575357
scope: {},
5358+
controllerAs: '$ctrl',
53585359
require: { container: '^parent', friend: 'sibling' },
5359-
controller: MeController
5360+
bindToController: true,
5361+
controller: MeController,
5362+
controllerAs: '$ctrl'
53605363
};
53615364
})
53625365
.directive('parent', function() {
@@ -5381,6 +5384,50 @@ describe('$compile', function() {
53815384
});
53825385
});
53835386

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

@@ -5404,7 +5451,9 @@ describe('$compile', function() {
54045451
restrict: 'E',
54055452
scope: {},
54065453
require: { container: '^parent', friend: 'sibling' },
5407-
controller: MeController
5454+
bindToController: true,
5455+
controller: MeController,
5456+
controllerAs: '$ctrl'
54085457
};
54095458
})
54105459
.directive('parent', function() {
@@ -5453,7 +5502,9 @@ describe('$compile', function() {
54535502
restrict: 'E',
54545503
scope: {},
54555504
require: { container: '^parent', friend: 'sibling' },
5456-
controller: MeController
5505+
bindToController: true,
5506+
controller: MeController,
5507+
controllerAs: '$ctrl'
54575508
};
54585509
})
54595510
.directive('parent', function() {

0 commit comments

Comments
 (0)