Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-options displays blank selected option, even though ng-model is defined

I have created a plunkr to emphasize the problem, perhaps it's because the source of the ng-repeat is a function, I am not sure, but so far I've tried everything in order to solve this, and couldn't mange.

plunkr: http://plnkr.co/edit/qQFsRM?p=preview

HTML

<html>

  <head>
    <script data-require="[email protected]" data-semver="1.2.0-rc1" src="http://code.angularjs.org/1.2.0rc1/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app='myApp' ng-controller='mainCtrl'>
  <ng-include src="'menu.html'">
  </ng-include>

</html>

Script

var app = angular.module('myApp', []);

app.controller('mainCtrl', function($scope, $httpBackend){
  $scope.model = {};
  $scope.model.myJobs = {};
  $scope.refreshJobs = function(){

  }
});

app.controller('menuCtrl', function($scope){

$scope.model.locations = function(){
  var loc = [];
  loc[1] = 'Dublin';
  loc[2] = 'Stockholm';
  loc[3] = 'New Jersy';
  $scope.model.selectedLocationDef = loc.indexOf('Dublin');
  return loc;
}
  $scope.model.selectedLocation =  $scope.model.selectedLocationDef;

$scope.$watch('model.selectedLocation', function(location){
  $scope.refreshJobs(location);
});

});
like image 253
Oleg Belousov Avatar asked Aug 20 '13 13:08

Oleg Belousov


People also ask

Why angular dropdown automatically adds an empty value?

Hi Nishu, I guess your problem is AngularJS Dropdown automatically adds an Empty Value and you want to remove that blank value. The empty option is generated when a value referenced by ng-model doesn't exist in a set of options passed to ng-options. This happens to prevent accidental model selection.

How do I set default Ng-options?

In my opinion the correct way to set a default value is to simply pre-fill your ng-model property with the value selected from your ng-options , angular does the rest. Essentially when you define the $scope property your select will bind to assign it the default value from your data array.

What is Ng selected?

Definition and Usage. The ng-selected directive sets the selected attribute of an <option> element in a <select> list. The option will be selected if the expression inside the ng-selected attribute returns true. The ng-selected directive is necessary to be able to shift the value between true and false .

How do ng-options work?

Definition and Usage. The ng-options directive fills a <select> element with <options>. The ng-options directive uses an array to fill the dropdown list. In many cases it would be easier to use the ng-repeat directive, but you have more flexibility when using the ng-options directive.


1 Answers

If you use an array as model, then the model is a string not a number. So you need to convert the number to string. Just try

$scope.model.selectedLocation = '1';
like image 192
zs2020 Avatar answered Nov 16 '22 01:11

zs2020