Skip to content Skip to sidebar Skip to footer

Ng-options Is Hiding The Blank Value With Angularjs V1.5

I followed the instructions specified in the V1.3 documentation to have a default option in one of my selects. Therefore in my Angular ̶1̶.̶3̶.̶1̶5̶   1.5.7 template I have

Solution 1:

The problem is with your controller.

Don't initialize your $scope.selectedObj.

Updated controller:

$scope.objects = [
  {
    id: '1',
    name: 'A',
  },
  {
    id: '2',
    name: 'B',
  },
  {
    id: '3',
    name: 'C',
  },
];

Here is a working example:

https://stackblitz.com/edit/angularjs-twzxrk?file=home%2Fhome.html

Solution 2:

Set the ng-model to null:

 ̶$̶s̶c̶o̶p̶e̶.̶s̶e̶l̶e̶c̶t̶e̶d̶O̶b̶j̶ ̶=̶ ̶'̶'̶;̶
 $scope.selectedObj = null;

The ng-option directive was heavily refactored in V1.4 and many bugs removed in V1.5. For more information, see

The DEMO

angular.module('app',[])
.controller("ctrl",function($scope) {
    $scope.selectedObj = null;
    $scope.objects = [
    {
        id: '1',
        name: 'A',
      },
      {
        id: '2',
        name: 'B',
      },
      {
        id: '3',
        name: 'C',
      },
    ];
})
<scriptsrc="//unpkg.com/angular/angular.js"></script><bodyng-app="app"ng-controller="ctrl"><selectng-model="selectedObj"class="form-control"ng-options="obj.id as obj.name for obj in objects"><optionvalue="">All objects</option></select></body>

Post a Comment for "Ng-options Is Hiding The Blank Value With Angularjs V1.5"