Skip to content

Commit 02f1a83

Browse files
petebacondarwinNarretz
authored andcommitted
fix($compile): ensure controllers with return value constructors are required correctly
See angular#13763 (comment)
1 parent f77a8e5 commit 02f1a83

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/ng/compile.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -2497,12 +2497,15 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
24972497
removeControllerBindingWatches =
24982498
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
24992499
}
2500+
}
25002501

2501-
if (isObject(controllerDirective.require) && !isArray(controllerDirective.require)) {
2502-
var controllerObj = getControllers(name, controllerDirective.require, $element, elementControllers);
2503-
extend(controller.instance, controllerObj);
2502+
// Bind the required controllers to the controller, if `require` is an object
2503+
forEach(controllerDirectives, function(controllerDirective, name) {
2504+
var require = controllerDirective.require;
2505+
if (!isArray(require) && isObject(require)) {
2506+
extend(elementControllers[name].instance, getControllers(name, require, $element, elementControllers));
25042507
}
2505-
}
2508+
});
25062509

25072510
// Trigger the `$onInit` method on all controllers that have one
25082511
forEach(elementControllers, function(controller) {

test/ng/compileSpec.js

+47
Original file line numberDiff line numberDiff line change
@@ -5430,6 +5430,53 @@ describe('$compile', function() {
54305430
});
54315431

54325432

5433+
it('should bind required controllers to controllers that return an explicit constructor return value', function() {
5434+
var parentController, containerController, siblingController, friendController, meController;
5435+
5436+
function MeController() {
5437+
this.name = 'Me';
5438+
this.$onInit = function() {
5439+
containerController = this.container;
5440+
friendController = this.friend;
5441+
};
5442+
}
5443+
function ParentController() {
5444+
return parentController = { name: 'Parent' };
5445+
}
5446+
function SiblingController() {
5447+
return siblingController = { name: 'Sibling' };
5448+
}
5449+
5450+
angular.module('my', [])
5451+
.directive('me', function() {
5452+
return {
5453+
restrict: 'E',
5454+
scope: {},
5455+
require: { container: '^parent', friend: 'sibling' },
5456+
controller: MeController
5457+
};
5458+
})
5459+
.directive('parent', function() {
5460+
return {
5461+
restrict: 'E',
5462+
scope: {},
5463+
controller: ParentController
5464+
};
5465+
})
5466+
.directive('sibling', function() {
5467+
return {
5468+
controller: SiblingController
5469+
};
5470+
});
5471+
5472+
module('my');
5473+
inject(function($compile, $rootScope, meDirective) {
5474+
element = $compile('<parent><me sibling></me></parent>')($rootScope);
5475+
expect(containerController).toEqual(parentController);
5476+
expect(friendController).toEqual(siblingController);
5477+
});
5478+
});
5479+
54335480
it('should require controller of an isolate directive from a non-isolate directive on the ' +
54345481
'same element', function() {
54355482
var IsolateController = function() {};

0 commit comments

Comments
 (0)