Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

feat(choices): add null option support #699

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/uiSelectChoicesDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
22 changes: 22 additions & 0 deletions src/uiSelectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down