在Angularjs中设置值选择

时间:2014-07-10 01:23:31

标签: javascript angularjs

我正在尝试将值绑定到下拉列表,我的数据是一个Object数组,但无法找到将选择的id绑定到下拉列表的方法。

这是我的代码和小提琴

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/

我想将id作为值。

感谢

1 个答案:

答案 0 :(得分:4)

使用as指定值。

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

jsfiddle