From 2cd5b4ec4409a818ccd33a6fbdeb99a3443a1809 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Thu, 2 Oct 2014 16:42:53 -0600 Subject: [PATCH] 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 --- src/ng/compile.js | 2 +- test/ng/compileSpec.js | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index b0d3cf1d..00f174d1 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1767,7 +1767,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { "Controller '{0}', required by directive '{1}', can't be found!", require, directiveName); } - return value; + return value || null; } else if (isArray(require)) { value = []; forEach(require, function(require) { diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 8e9fac55..294140bd 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -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() { directive('dep', function(log) { return { require: '?^main', link: function(scope, element, attrs, controller) { - log('dep:' + !!controller); + log('dep:' + controller); } }; }); }); inject(function(log, $compile, $rootScope) { $compile('
')($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() { directive('dep', function(log) { return { require: '^?main', link: function(scope, element, attrs, controller) { - log('dep:' + !!controller); + log('dep:' + controller); } }; }); }); inject(function(log, $compile, $rootScope) { $compile('
')($rootScope); - expect(log).toEqual('dep:false'); + expect(log).toEqual('dep:null'); }); });