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

Commit 8caf180

Browse files
committed
fix(compile): assign ctrl return values correctly for multiple directives
Fixes #12029 Closes #12036
1 parent 91b6022 commit 8caf180

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/ng/compile.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2007,9 +2007,12 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20072007
for (i in elementControllers) {
20082008
controller = elementControllers[i];
20092009
var controllerResult = controller();
2010+
20102011
if (controllerResult !== controller.instance) {
2012+
// If the controller constructor has a return value, overwrite the instance
2013+
// from setupControllers and update the element data
20112014
controller.instance = controllerResult;
2012-
$element.data('$' + directive.name + 'Controller', controllerResult);
2015+
$element.data('$' + i + 'Controller', controllerResult);
20132016
if (controller === controllerForBindings) {
20142017
// Remove and re-install bindToController bindings
20152018
thisLinkFn.$$destroyBindings();

test/ng/compileSpec.js

+35
Original file line numberDiff line numberDiff line change
@@ -4592,6 +4592,41 @@ describe('$compile', function() {
45924592
});
45934593

45944594

4595+
it('should correctly assign controller return values for multiple directives', function() {
4596+
var directiveController, otherDirectiveController;
4597+
module(function() {
4598+
4599+
directive('myDirective', function(log) {
4600+
return {
4601+
scope: true,
4602+
controller: function($scope) {
4603+
return directiveController = {
4604+
foo: 'bar'
4605+
};
4606+
}
4607+
};
4608+
});
4609+
4610+
directive('myOtherDirective', function(log) {
4611+
return {
4612+
controller: function($scope) {
4613+
return otherDirectiveController = {
4614+
baz: 'luh'
4615+
};
4616+
}
4617+
};
4618+
});
4619+
4620+
});
4621+
4622+
inject(function(log, $compile, $rootScope) {
4623+
element = $compile('<my-directive my-other-directive></my-directive>')($rootScope);
4624+
expect(element.data('$myDirectiveController')).toBe(directiveController);
4625+
expect(element.data('$myOtherDirectiveController')).toBe(otherDirectiveController);
4626+
});
4627+
});
4628+
4629+
45954630
it('should get required parent controller', function() {
45964631
module(function() {
45974632
directive('nested', function(log) {

0 commit comments

Comments
 (0)