AngularJS不会更新所选选项

时间:2015-01-26 22:33:49

标签: angularjs-scope ng-options angularjs-ng-options

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

$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>

我该如何解决?

由于

1 个答案:

答案 0 :(得分:0)

您使用&#39;选择&#39;和&#39;跟踪&#39;一起,但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/