I have a form with a select, generated via ng-options and hooked up with a ng-model.
This select calls a function via ng-change.
In this function I remove the selected option from the select and reset the value in the referenced ng-model to the default value.
On a later point I want to add this option back into the select, which works fine, but when adding the option to the select it automatically selects it, instead of the default value, that is already selected.
This seems to happen after the function adding the option finished and angular is running its internal $apply function.
How can I keep the default value selected?
html:
<select ng-model="selectedItem" ng-options="item.name for item in allItems" ng-change="selectItem(selectedItem)">
<option value="">default</option>
</select>
angularjs:
$scope.selectItem = function(selectedItem) {
$scope.allItems.splice( $scope.allItems.indexOf(selectedItem), 1 );
}
$scope.addItem = function(item) {
// $scope.selectedItem == "";
$scope.allItems.push(item);
// $scope.selectedItem == "";
}