1

I have made dropdown with ng-option and added grouping. Data contains empty string for some opting on which i have added grup by. It show two empty nodes in dropdown. Need to remove those two empty nodes from dropdown

angular.module('selectExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.colors = [{
      name: 'black',
      shade: ''
    }, {
      name: 'white',
      shade: 'light',
      notAnOption: true
    }, {
      name: 'red',
      shade: 'dark'
    }, {
      name: 'blue',
      shade: 'dark',
      notAnOption: true
    }, {
      name: 'yellow',
      shade: 'light',
      notAnOption: false
    }];
    $scope.myColor = $scope.colors[2]; // red
  }]);

Html:

<select ng-model="myColor" ng-options="color.name group by color.shade for color in colors">
  </select>

Plunker link

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Gagan
  • 130
  • 9

1 Answers1

1

You can simply remove those empties shades of colors.

Just include this in your controller:

$scope.colors = $scope.colors.map(function(value) {
  if (value.shade == '') {
    value.shade = undefined;
  }
  return value;
});
developer033
  • 24,267
  • 8
  • 82
  • 108