0

我有以下问题。在我的控制器中,我有这个变量:

$scope.types = [
    {id: 0, name: $translate.instant("FIRST")},
    {id: 1, name: $translate.instant("SECOND")},
    {id: 2, name: $translate.instant("THIRD")},
    {id: 3, name: $translate.instant("FOURTH")}
];
$scope.itemtype = {};
$scope.itemtype = $scope.types[0];

在我看来,我有这个选择

            <select class="form-control" 
                    required
                    ng-model="itemtype"
                    ng-options="item as item.name for item in types track by item.id">
            </select>

我有类似的选择可以完美地工作,但这不起作用,如果我在模型中选择第二个选项,旧值仍然处于选中状态,但在资源管理器中,我将第二个选项视为已选中。

在萤火虫中,我正在查看此输出

        <select class="form-control ng-pristine ng-valid ng-valid-required ng-touched" ng-options="item as item.name for item in types track by item.id" ng-model="itemtype" required="" tabindex="0" aria-required="false" aria-invalid="false">
        <option value="0" label="First">First</option>
        <option value="1" label="Second">Second</option>
        <option value="2" label="Third"> Third </option>
        <option value="3" selected="selected" label="Fourth"> Fourth </option>
        </select>

我该如何解决?

谢谢

4

1 回答 1

0

您一起使用了 'select as' 和 'track by',但 API 文档说:

不要在同一个表达式中使用 select as 和 track by。它们并非旨在协同工作。

参考:

https://code.angularjs.org/1.3.10/docs/api/ng/directive/select

http://gurustop.net/blog/2014/01/28/common-problems-and-solutions-when-using-select-elements-with-angular-js-ng-options-initial-selection/

于 2015-02-02T08:28:02.183 回答