I made a directive to handle this ... I called it "empty-option":
myApp.directive("emptyOption", ["$timeout", function ($timeout) {
return {
restrict: "A",
require: "^ngModel",
link: function (scope, element, attrs, ngModelCtrl) {
var parentSelectDom = element[0].parentNode,
removed = false;
if (parentSelectDom.nodeName === "SELECT") {
scope.$watch(function () {
return ngModelCtrl.$modelValue;
}, function (newVal, oldVal) {
if (newVal === undefined) {
if (removed) {
$timeout(function () {
parentSelectDom.add(element[0], parentSelectDom[0]);
}, 0);
removed = false;
}
}
else if (!removed) {
$timeout(function () {
parentSelectDom.remove(0);
}, 0);
removed = true;
}
});
}
}
}
}]);
The directive may indicate an empty option for selection. It removes the parameter when selected and adds the empty option back when the model value is cleared.
Fiddle is here .
source
share