diff --git a/src/uiSelectChoicesDirective.js b/src/uiSelectChoicesDirective.js index b61bf89b0..5d0716b79 100644 --- a/src/uiSelectChoicesDirective.js +++ b/src/uiSelectChoicesDirective.js @@ -26,6 +26,11 @@ uis.directive('uiSelectChoices', $select.disableChoiceExpression = attrs.uiDisableChoice; $select.onHighlightCallback = attrs.onHighlight; + // null-option support + if (angular.isDefined(attrs.nullOption)) { + $select.nullOption = scope.$eval(attrs.nullOption); + $select.looseNull = angular.isDefined(attrs.looseNull); + } if(groupByExp) { var groups = element.querySelectorAll('.ui-select-choices-group'); diff --git a/src/uiSelectDirective.js b/src/uiSelectDirective.js index 0809d6c14..44ad5bd82 100644 --- a/src/uiSelectDirective.js +++ b/src/uiSelectDirective.js @@ -121,6 +121,28 @@ uis.directive('uiSelect', return inputValue; }); + //From view --> model (null-option support) + ngModel.$parsers.push(function (inputValue) { + // add a parser to convert the null option + // into an actual null value when it is selected + // (this should run after the main parser) + return angular.isDefined($select.nullOption) && inputValue === $select.nullOption ? null : inputValue; + }); + + //From model --> view (null-option support) + ngModel.$formatters.unshift(function (inputValue) { + // loose-null option (treat undefined the same as null) + var isNull = $select.looseNull ? + /*jshint eqnull:true */ + function (value) { return value == null; } : + function (value) { return value === null; }; + + // add a formatter to convert a null value + // into the null option when the model is set + // (this should run before the main formatter) + return angular.isDefined($select.nullOption) && isNull(inputValue) ? $select.nullOption : inputValue; + }); + //Set reference to ngModel from uiSelectCtrl $select.ngModel = ngModel;