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

Commit fe45f49

Browse files
author
Kent C. Dodds
committed
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. Closes #9404
1 parent 4915363 commit fe45f49

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/ng/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
17641764
"Controller '{0}', required by directive '{1}', can't be found!",
17651765
require, directiveName);
17661766
}
1767-
return value;
1767+
return value || null;
17681768
} else if (isArray(require)) {
17691769
value = [];
17701770
forEach(require, function(require) {

test/ng/compileSpec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -4095,38 +4095,38 @@ describe('$compile', function() {
40954095
});
40964096

40974097

4098-
it("should not throw an error if required controller can't be found and is optional",function() {
4098+
it("should pass null if required controller can't be found and is optional",function() {
40994099
module(function() {
41004100
directive('dep', function(log) {
41014101
return {
41024102
require: '?^main',
41034103
link: function(scope, element, attrs, controller) {
4104-
log('dep:' + !!controller);
4104+
log('dep:' + controller);
41054105
}
41064106
};
41074107
});
41084108
});
41094109
inject(function(log, $compile, $rootScope) {
41104110
$compile('<div main><div dep></div></div>')($rootScope);
4111-
expect(log).toEqual('dep:false');
4111+
expect(log).toEqual('dep:null');
41124112
});
41134113
});
41144114

41154115

4116-
it("should not throw an error if required controller can't be found and is optional with the question mark on the right",function() {
4116+
it("should pass null if required controller can't be found and is optional with the question mark on the right",function() {
41174117
module(function() {
41184118
directive('dep', function(log) {
41194119
return {
41204120
require: '^?main',
41214121
link: function(scope, element, attrs, controller) {
4122-
log('dep:' + !!controller);
4122+
log('dep:' + controller);
41234123
}
41244124
};
41254125
});
41264126
});
41274127
inject(function(log, $compile, $rootScope) {
41284128
$compile('<div main><div dep></div></div>')($rootScope);
4129-
expect(log).toEqual('dep:false');
4129+
expect(log).toEqual('dep:null');
41304130
});
41314131
});
41324132

0 commit comments

Comments
 (0)