Praveen Raj
Praveen Raj

Reputation: 1014

How to remove empty option in a select box while dynamically removing the option

I am learning AngularJs for couple of weeks.I tried a snippet with select box.First i face empty option issue while loading I googled and solved.Now if i remove the option by dynamically the same empty option comes and i am getting this Error "value is undefined" This is my issue.Thanks in advance

Upvotes: 0

Views: 232

Answers (1)

Anton Telesh
Anton Telesh

Reputation: 3842

You have removed empty option at init by assigning $scope.selected property to one of $scope.opt1 items.

You have to to this another time after removing an item from that list. It's because you have removed selected item and now your $scope.selected property doesn't match any of existing properties.

$scope.addOption = function(){

  if($scope.selected !== undefined){
    $scope.opt2.push($scope.selected);
    $scope.opt1 = $scope.opt1.filter(function(value) {
      return $scope.selected.name !== value.name;
    });
    $scope.selected = $scope.opt1[0];
  }

};

Also I have changed $.each to Array.prototype.filter to prevent error throwing.

Upvotes: 1

Related Questions