角度ng-repeat不选择绑定值

时间:2014-06-30 11:39:46

标签: angularjs html-select ng-repeat

我有一个应该被选入SELECT html元素的关键字段。此SELECT填充了 ng-repeat

我定义了两个SELECT,一个是ng-repeat,另一个是ng-options:

<div ng-controller="MyDisplayCtrl">
    <select ng-model="context.id1">
        <option value="">-</option>
        <option ng-repeat="i in lista" value="{{i.id}}">{{i.value}}</option>
    </select> Should have "three" selected, but shows - <br/>

    <select ng-model="context.id2" ng-options="i.value for i in lista">
    </select> Selects "three" automatically <br/>
</div>

我的控制器是这样的:

function MyDisplayCtrl($scope) {
    var x1 = {id: 1, value: 'one'};
    var x2 = {id: 2, value: 'two'};
    var x3 = {id: 3, value: 'three'};
    window.setTimeout(function() {
        $scope.$apply(function() {
            $scope.lista = [x1, x2, x3];
        });
    }, 2000);

    $scope.context = {id1: 3, id2: x3};
}

我有这个列表由AJAX带到客户端,所以我在这里使用了 setTimeout()

使用ng-options的方法有效,但我不会有一个对象,唯一的关键。

看看这个小提琴:http://jsfiddle.net/nCG2H/

问题:我试图理解为什么第一个SELECT不起作用,算法是因为我根据AJAX响应的时序让它工作。

1 个答案:

答案 0 :(得分:2)

请使用ng-selected =&#34; {{i.id}}&#34;代替值=&#34; {{i.id}}&#34; ...

相关问题