无法在angularjs中加载下拉列表的ng-model

时间:2014-03-13 08:44:43

标签: javascript angularjs angularjs-ng-repeat

我有items作为我的json对象,其中包含一些我想在下拉列表中显示的数据。
但它仍然显示我的下拉列表为空白。
我在这里使用ng-repeat。
I have listed my code here.
HTML:

<div ng-controller="Ctrl">   
    <div ng-repeat="item in items">
        <select ng-model="item.shareToOption" ng-options="c.value for c in shareToOptions"></select>
    </div> 
<div>

JS:

var app = angular.module('app', []);

function Ctrl($scope) {
        $scope.items = [
            {
            "shareToOption" : {id:1,value:"AA1"}
            },
            {
            "shareToOption" : {id:2,value:"AA2"},
            },
            {
            "shareToOption" : {id:3,value:"AA3"},
            },
            {
            "shareToOption" : {id:4,value:"AA4"}
            }
        ];

        $scope.shareToOptions = [
            {id:1,value:"AA1"},
            {id:2,value:"AA2"},
            {id:3,value:"AA3"},
            {id:4,value:"AA4"},
            {id:4,value:"AA5"},
            {id:4,value:"AA6"},
            {id:4,value:"AA7"}
        ];
}

1 个答案:

答案 0 :(得分:2)

您应该为track by使用新的ng-options表达式。像这样:

<div ng-controller="Ctrl">   
    <div ng-repeat="item in items">
        <select 
          ng-model="item.shareToOption" 
          ng-options="c.value for c in shareToOptions track by c.id"
        ></select>
    </div> 
<div>

确保您使用的是Angular 1.2。