Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: Select with ng-options- how to pass value as integer?

I am having an issue with my select form. When I select any option, the value that is being passed is of a string type, instead of number type. Here is my object that I use for ng-repeat:

$scope.apples ={
         0:'Apple0',
         1:'Apple1',
         2:'Apple2',
         3:'Apple3'
};

And here is my select, which passes string, instead of number:

 <select ng-model="test" ng-options="key as value for (key,value) in apples"></select>

Any ideas?

like image 798
uksz Avatar asked Dec 24 '22 12:12

uksz


1 Answers

Every time you're iterating on object fields, IMO, there is a design problem. Use an array:

$scope.apples = [
  {
    id: 0,
    name: 'Apple0'
  },
  {
    id: 1,
    name: 'Apple1'
  },
  ...
];

and in the view:

ng-options="apple.id as apple.name for apple in apples"
like image 152
JB Nizet Avatar answered Dec 28 '22 10:12

JB Nizet