From 7f3f3dd3ebcc44711600ac292af54c411c3c705f Mon Sep 17 00:00:00 2001 From: Martin Staffa Date: Fri, 25 Sep 2015 23:53:37 +0200 Subject: [PATCH] fix(ngOptions): skip comments when looking for option elements When the empty/blank option has a directive that transcludes, ngIf for example, a comment will be added into the select. Previously, ngOptions used this comment as the empty option, which would mess up the displayed options. Closes #12190 --- src/ng/directive/ngOptions.js | 5 ++++- test/ng/directive/ngOptionsSpec.js | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/ng/directive/ngOptions.js b/src/ng/directive/ngOptions.js index 405c8879da3f..282dff3aedd2 100644 --- a/src/ng/directive/ngOptions.js +++ b/src/ng/directive/ngOptions.js @@ -626,7 +626,10 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) { if (emptyOption_ || unknownOption_) { while (current && (current === emptyOption_ || - current === unknownOption_)) { + current === unknownOption_ || + emptyOption_ && emptyOption_.nodeType === NODE_TYPE_COMMENT)) { + // Empty options might have directives that transclude + // and insert comments (e.g. ngIf) current = current.nextSibling; } } diff --git a/test/ng/directive/ngOptionsSpec.js b/test/ng/directive/ngOptionsSpec.js index b82440b4d3e9..bc50bcbef818 100644 --- a/test/ng/directive/ngOptionsSpec.js +++ b/test/ng/directive/ngOptionsSpec.js @@ -2095,6 +2095,30 @@ describe('ngOptions', function() { expect(element[0].selectedIndex).toEqual(0); expect(scope.selected).toEqual([]); }); + + + it('should be possible to use ngIf in the blank option', function() { + var option; + createSingleSelect(''); + + scope.$apply(function() { + scope.values = [{name: 'A'}]; + scope.isBlank = true; + }); + + expect(element.find('option').length).toBe(2); + option = element.find('option').eq(0); + expect(option.val()).toBe(''); + expect(option.text()).toBe('blank'); + + scope.$apply(function() { + scope.isBlank = false; + }); + + expect(element.find('option').length).toBe(1); + option = element.find('option').eq(0); + expect(option.text()).toBe('A'); + }); });