fix($compile): returning null when an optional controller is not found

Currently, `undefined` is returned. However, the desired behavior is to
return `null` when the controller is optional and not found.

(If this breaks your app, it really shouldn't .v.)

Closes #9404
Closes #9392
This commit is contained in:
Kent C. Dodds
2014-10-02 16:42:53 -06:00
committed by Caitlin Potter
parent 4bf254c155
commit 2cd5b4ec44
2 changed files with 7 additions and 7 deletions

View File

@@ -1767,7 +1767,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
"Controller '{0}', required by directive '{1}', can't be found!", "Controller '{0}', required by directive '{1}', can't be found!",
require, directiveName); require, directiveName);
} }
return value; return value || null;
} else if (isArray(require)) { } else if (isArray(require)) {
value = []; value = [];
forEach(require, function(require) { forEach(require, function(require) {

View File

@@ -4197,38 +4197,38 @@ describe('$compile', function() {
}); });
it("should not throw an error if required controller can't be found and is optional",function() { it("should pass null if required controller can't be found and is optional",function() {
module(function() { module(function() {
directive('dep', function(log) { directive('dep', function(log) {
return { return {
require: '?^main', require: '?^main',
link: function(scope, element, attrs, controller) { link: function(scope, element, attrs, controller) {
log('dep:' + !!controller); log('dep:' + controller);
} }
}; };
}); });
}); });
inject(function(log, $compile, $rootScope) { inject(function(log, $compile, $rootScope) {
$compile('<div main><div dep></div></div>')($rootScope); $compile('<div main><div dep></div></div>')($rootScope);
expect(log).toEqual('dep:false'); expect(log).toEqual('dep:null');
}); });
}); });
it("should not throw an error if required controller can't be found and is optional with the question mark on the right",function() { it("should pass null if required controller can't be found and is optional with the question mark on the right",function() {
module(function() { module(function() {
directive('dep', function(log) { directive('dep', function(log) {
return { return {
require: '^?main', require: '^?main',
link: function(scope, element, attrs, controller) { link: function(scope, element, attrs, controller) {
log('dep:' + !!controller); log('dep:' + controller);
} }
}; };
}); });
}); });
inject(function(log, $compile, $rootScope) { inject(function(log, $compile, $rootScope) {
$compile('<div main><div dep></div></div>')($rootScope); $compile('<div main><div dep></div></div>')($rootScope);
expect(log).toEqual('dep:false'); expect(log).toEqual('dep:null');
}); });
}); });