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

Commit 2cd5b4e

Browse files
Kent C. Doddscaitp
Kent C. Dodds
authored andcommitted
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
1 parent 4bf254c commit 2cd5b4e

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
@@ -1767,7 +1767,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
17671767
"Controller '{0}', required by directive '{1}', can't be found!",
17681768
require, directiveName);
17691769
}
1770-
return value;
1770+
return value || null;
17711771
} else if (isArray(require)) {
17721772
value = [];
17731773
forEach(require, function(require) {

test/ng/compileSpec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -4197,38 +4197,38 @@ describe('$compile', function() {
41974197
});
41984198

41994199

4200-
it("should not throw an error if required controller can't be found and is optional",function() {
4200+
it("should pass null if required controller can't be found and is optional",function() {
42014201
module(function() {
42024202
directive('dep', function(log) {
42034203
return {
42044204
require: '?^main',
42054205
link: function(scope, element, attrs, controller) {
4206-
log('dep:' + !!controller);
4206+
log('dep:' + controller);
42074207
}
42084208
};
42094209
});
42104210
});
42114211
inject(function(log, $compile, $rootScope) {
42124212
$compile('<div main><div dep></div></div>')($rootScope);
4213-
expect(log).toEqual('dep:false');
4213+
expect(log).toEqual('dep:null');
42144214
});
42154215
});
42164216

42174217

4218-
it("should not throw an error if required controller can't be found and is optional with the question mark on the right",function() {
4218+
it("should pass null if required controller can't be found and is optional with the question mark on the right",function() {
42194219
module(function() {
42204220
directive('dep', function(log) {
42214221
return {
42224222
require: '^?main',
42234223
link: function(scope, element, attrs, controller) {
4224-
log('dep:' + !!controller);
4224+
log('dep:' + controller);
42254225
}
42264226
};
42274227
});
42284228
});
42294229
inject(function(log, $compile, $rootScope) {
42304230
$compile('<div main><div dep></div></div>')($rootScope);
4231-
expect(log).toEqual('dep:false');
4231+
expect(log).toEqual('dep:null');
42324232
});
42334233
});
42344234

0 commit comments

Comments
 (0)