如何在Angular中为select标签元素设置选项值?

时间:2014-07-15 09:30:56

标签: angularjs

我需要为渲染的选择标记设置选项值yo id,使用以下示例我可以将选项值设置为0,1,2。

知道我在这里做错了什么吗?

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>

1 个答案:

答案 0 :(得分:1)

视图

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

控制器

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

更新了 Fiddle