1

Here is my JSON array:

array=[ {id:1, name:'A'} , {id:2, name:'B'} ]

And in ng-model I am setting value which may be string like

modelValue='1'

I had ng-option

ng-option="option.id as option.name for option in array"
ng-model="modelValue"

But it is not showing selected value of ng-model in select

Mistalis
  • 17,793
  • 13
  • 73
  • 97
Pooja-G
  • 518
  • 4
  • 20

5 Answers5

2

And in ng-model I am setting value which may be string like

You should provide the same primitive or object type to your $scope.modelValue as of id. This is the preferred way.

If only the JSON array is being fetched dynamically AND type consistency is not guaranteed then you can apply a function to convert it to the type that is set to $scope.modelValue

In HTML

<select ng-model="modelValue" ng-options="getId(option.id) as option.name for option in myarray"> 

</select>

In Controller

$scope.modelValue='1';

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

$scope.getId = function(id)
{
   if(typeof $scope.modelValue == "string")
     return String(id);
   else if(typeof $scope.modelValue == "number")
     return parseInt(id);
}

COMPLETE EXAMPLE

Also see this answer

Community
  • 1
  • 1
Nishant123
  • 1,968
  • 2
  • 26
  • 44
0

Here is a working JSFiddle example based on your description: http://jsfiddle.net/9pLdgmm1/1/

You should use ng-options instead of ng-option

<select ng-options="p.name for p in people"
        ng-model="selectedPerson">
</select>
Tome Pejoski
  • 1,582
  • 2
  • 10
  • 34
0

You should take ng-options instead of ng-option, als you should take integer itself instead of string..

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<select ng-model="modelValue" ng-options="option.id as option.name for option in names">
</select>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.names = [ {id:1, name:'A'} , {id:2, name:'B'} ]
    $scope.modelValue = 1;
});
</script>

<p>This example shows how to fill a dropdown list using the ng-options directive.</p>

</body>
</html>

Please run the above snippet

Here is a Working DEMO

Sravan
  • 18,467
  • 3
  • 30
  • 54
0

This should not be selected, because of all id is in numbers. If you want to make it select, then parse your $scope.modelValue to numbers like this.

$scope.modelValue= parseInt("1");

Also change your ng-options instead of ng-option

Masum Billah
  • 2,209
  • 21
  • 20
-1

You have a syntax error in ng-options:

<select ng-options="option.name for option in array" ng-model="modelValue"></select>

var myapp = angular.module('myapp', []);
myapp.controller('FirstCtrl', function($scope) {
  $scope.array = [{
    id: 1,
    name: 'A'
  }, {
    id: 2,
    name: 'B'
  }];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">
  <fieldset ng-controller="FirstCtrl">
    <select ng-options="option.name for option in array" ng-model="modelValue"></select>
    modelValue is: {{modelValue}}
  </fieldset>
</div>
Mistalis
  • 17,793
  • 13
  • 73
  • 97