Skip to content

Commit d4acbf0

Browse files
committed
fix(compile): assign controller return value to correct ctrls
Fixes angular#12029
1 parent 41385f0 commit d4acbf0

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/ng/compile.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1997,9 +1997,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19971997
for (i in elementControllers) {
19981998
controller = elementControllers[i];
19991999
var controllerResult = controller();
2000+
20002001
if (controllerResult !== controller.instance) {
2002+
// Assign controller return value to the instance
20012003
controller.instance = controllerResult;
2002-
$element.data('$' + directive.name + 'Controller', controllerResult);
2004+
$element.data('$' + i + 'Controller', controllerResult);
20032005
if (controller === controllerForBindings) {
20042006
// Remove and re-install bindToController bindings
20052007
thisLinkFn.$$destroyBindings();

test/ng/compileSpec.js

+35
Original file line numberDiff line numberDiff line change
@@ -4573,6 +4573,41 @@ describe('$compile', function() {
45734573
});
45744574

45754575

4576+
it('should correctly assign controller return values for multiple directives', function() {
4577+
var directiveController, otherDirectiveController;
4578+
module(function() {
4579+
4580+
directive('myDirective', function(log) {
4581+
return {
4582+
scope: true,
4583+
controller: function($scope) {
4584+
return directiveController = {
4585+
foo: 'bar'
4586+
};
4587+
}
4588+
};
4589+
});
4590+
4591+
directive('myOtherDirective', function(log) {
4592+
return {
4593+
controller: function($scope) {
4594+
return otherDirectiveController = {
4595+
baz: 'luh'
4596+
};
4597+
}
4598+
};
4599+
});
4600+
4601+
});
4602+
4603+
inject(function(log, $compile, $rootScope) {
4604+
element = $compile('<my-directive my-other-directive></my-directive>')($rootScope);
4605+
expect(element.data('$myDirectiveController')).toBe(directiveController);
4606+
expect(element.data('$myOtherDirectiveController')).toBe(otherDirectiveController);
4607+
});
4608+
});
4609+
4610+
45764611
it('should get required parent controller', function() {
45774612
module(function() {
45784613
directive('nested', function(log) {

0 commit comments

Comments
 (0)