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

respect explicit return values from controller functions (fixes #11147) #11326

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1967,13 +1967,15 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
for (i in elementControllers) {
controller = elementControllers[i];
var controllerResult = controller();
if (controllerResult !== controller.instance &&
controller === controllerForBindings) {
// Remove and re-install bindToController bindings
thisLinkFn.$$destroyBindings();
thisLinkFn.$$destroyBindings =
initializeDirectiveBindings(scope, attrs, controllerResult,
bindings, scopeDirective);
if (controllerResult !== controller.instance) {
controller.instance = controllerResult;
$element.data('$' + directive.name + 'Controller', controllerResult);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are a few different circumstances that this happens, so we might need to be a bit more careful about this --- be sure to include test coverage that covers controllers for transclude directives too

if (controller === controllerForBindings) {
// Remove and re-install bindToController bindings
thisLinkFn.$$destroyBindings();
thisLinkFn.$$destroyBindings =
initializeDirectiveBindings(scope, attrs, controllerResult, bindings, scopeDirective);
}
}
}
}
Expand Down
124 changes: 124 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4197,6 +4197,130 @@ describe('$compile', function() {
});


it('should respect explicit return value from controller', function() {
module(function() {
directive('logControllerProp', function(log) {
return {
controller: function($scope) {
this.foo = 'baz'; // value should not be used.
return {foo: 'bar'};
},
link: function(scope, element, attrs, controller) {
log(controller.foo);
}
};
});
});
inject(function(log, $compile, $rootScope) {
element = $compile('<log-controller-prop></log-controller-prop>')($rootScope);
expect(log).toEqual('bar');
expect(element.data('$logControllerPropController').foo).toEqual('bar');
});
});


it('should get explicit return value of required parent controller', function() {
module(function() {
directive('nested', function(log) {
return {
require: '^^?nested',
controller: function() {
return {foo: 'bar'};
},
link: function(scope, element, attrs, controller) {
log(!!controller && controller.foo);
}
};
});
});
inject(function(log, $compile, $rootScope) {
element = $compile('<div nested><div nested></div></div>')($rootScope);

expect(log).toEqual('bar; false');
});
});


it('should respect explicit controller return value when using controllerAs', function() {
module(function() {
directive('main', function() {
return {
templateUrl: 'main.html',
scope: {},
controller: function() {
this.name = 'lucas';
return {name: 'george'};
},
controllerAs: 'mainCtrl'
};
});
});
inject(function($templateCache, $compile, $rootScope) {
$templateCache.put('main.html', '<span>template:{{mainCtrl.name}}</span>');
element = $compile('<main/>')($rootScope);
$rootScope.$apply();
expect(element.text()).toBe('template:george');
});
});


it('transcluded children should receive explicit return value of parent controller', function() {
var expectedController;
module(function() {
directive('nester', valueFn({
transclude: 'content',
controller: function($transclude) {
this.foo = 'baz';
expectedController = {transclude:$transclude, foo: 'bar'};
return expectedController;
},
link: function(scope, el, attr, ctrl) {
ctrl.transclude(cloneAttach);
function cloneAttach(clone) {
el.append(clone);
}
}
}));
directive('nested', function(log) {
return {
require: '^^nester',
link: function(scope, element, attrs, controller) {
expect(controller).toBe(expectedController);
log('done');
}
};
});
});
inject(function(log, $compile) {
element = $compile('<div nester><div nested></div></div>')($rootScope);
$rootScope.$apply();
expect(log).toEqual('done');
});
});


it('explicit controller return values are ignored if they are primitives', function() {
module(function() {
directive('logControllerProp', function(log) {
return {
controller: function($scope) {
this.foo = 'baz'; // value *will* be used.
return 'bar';
},
link: function(scope, element, attrs, controller) {
log(controller.foo);
}
};
});
});
inject(function(log, $compile, $rootScope) {
element = $compile('<log-controller-prop></log-controller-prop>')($rootScope);
expect(log).toEqual('baz');
expect(element.data('$logControllerPropController').foo).toEqual('baz');
});
});


it('should get required parent controller', function() {
module(function() {
directive('nested', function(log) {
Expand Down