GibboK
GibboK

Reputation: 73918

How to setup option value for a select tag element in Angular?

I need to set the option value yo id for the rendered select tag, using the following example I can set the option value as 0, 1, ,2.

Any idea what am I doing wrong here?

http://jsfiddle.net/GFF6P/1/

function ctrl($scope) {
    $scope.devices = [{
        name: "pc1",
        id: 10
    }, {
        name: "pc2",
        id: 20
    }, {
        name: "pc3",
        id: 30
    }];

    $scope.selectedDevice = $scope.devices[0];
}

<div ng-app ng-controller="ctrl">
    <select data-ng-model="selectedDevice" name="devices" data-ng-required="true" data-ng-options="device.name for device in devices"></select>
</div>

Upvotes: 0

Views: 135

Answers (1)

AlwaysALearner
AlwaysALearner

Reputation: 43947

View

<select data-ng-model="selectedDevice" 
name="devices" 
data-ng-required="true" 
data-ng-options="device.id as device.name for device in devices">
</select>

Controller

$scope.selectedDevice = $scope.devices[0].id;

Updated Fiddle

Upvotes: 1

Related Questions