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

2
  • typo : ng-optionS, you forgot the 's'. Commented Dec 2, 2016 at 10:23
  • @Walfrat That was typo while typing in stack Commented Dec 2, 2016 at 13:01

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

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>

2 Comments

I have integer value in ng-option and string in model.
Thank you. But I got the perfect solution what I want by Nishant123
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

Comments

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

Comments

-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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.