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

Commit 57fe701

Browse files
committed
fix(compile): respect explicit return values from controller functions
When controller functions return an explicit value that value should be what is passed to the linking functions, and to any child/sibling controllers that `require` it. It should also be bound to the data store on the dom element. Closes #11147
1 parent b89320a commit 57fe701

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/ng/compile.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1967,13 +1967,16 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19671967
for (i in elementControllers) {
19681968
controller = elementControllers[i];
19691969
var controllerResult = controller();
1970-
if (controllerResult !== controller.instance &&
1971-
controller === controllerForBindings) {
1972-
// Remove and re-install bindToController bindings
1973-
thisLinkFn.$$destroyBindings();
1974-
thisLinkFn.$$destroyBindings =
1970+
if (controllerResult !== controller.instance && (isObject(controllerResult) || isFunction(controllerResult))) {
1971+
controller.instance = controllerResult;
1972+
$element.data('$' + directive.name + 'Controller', controllerResult);
1973+
if (controller === controllerForBindings) {
1974+
// Remove and re-install bindToController bindings
1975+
thisLinkFn.$$destroyBindings();
1976+
thisLinkFn.$$destroyBindings =
19751977
initializeDirectiveBindings(scope, attrs, controllerResult,
1976-
bindings, scopeDirective);
1978+
bindings, scopeDirective);
1979+
}
19771980
}
19781981
}
19791982
}

test/ng/compileSpec.js

+1
Original file line numberDiff line numberDiff line change
@@ -4214,6 +4214,7 @@ describe('$compile', function() {
42144214
inject(function(log, $compile, $rootScope) {
42154215
element = $compile('<log-controller-prop></log-controller-prop>')($rootScope);
42164216
expect(log).toEqual('bar');
4217+
expect(element.data('$logControllerPropController').foo).toEqual('bar');
42174218
});
42184219
});
42194220

0 commit comments

Comments
 (0)