1

I'm trying to bind values to dropdown list, my data is an Object array but can't find a way to bind the id of the selection to the dropdown value.

Here is my code and fiddle

var app = angular.module('myApp', []);
function Ctrl($scope) {
    $scope.optionSource = [{
        "name" : "First Cost",
        "id" : "1"

        }, {
        "name" : "Second",
        "id" : "3"

        }, {
        "name" : "Third",
        "id" : "2"

        }, {
        "name" : "Sone",
        "id" : "5"

        }, {
        "name" : "List  CC",
        "id" : "4"
    }];
    $scope.value = [2,4];
}

html

<select multiple="multiple" ng-model="value"
    ng-options="option.name for option in optionSource">
</select>

http://jsfiddle.net/7RUzu/1/

I want the id to be the value.

thanks

aw04
  • 10,857
  • 10
  • 56
  • 89
MZH
  • 1,414
  • 4
  • 29
  • 57

1 Answers1

4

Use as to specify the value.

<select multiple="multiple" ng-model="value"
    ng-options="option.id as option.name for option in optionSource">
</select>

jsfiddle

aw04
  • 10,857
  • 10
  • 56
  • 89
  • thanks man, but can we make the value 1,3,2,5,4 instead of 0,1,2,3,3 screencast.com/t/yRlycnZN – MZH Jul 10 '14 at 13:45
  • @MZH It works that way by design. Don't fight the system :) Read the comments on the accepted answer [here](http://stackoverflow.com/questions/12139152/how-to-set-value-property-in-angularjs-ng-options) for an explanation – aw04 Jul 10 '14 at 14:02