当模型值为数字0时,角度ng模型不呈现选择选项

时间:2015-02-21 21:52:54

标签: angularjs angular-ngmodel ng-options

我有以下测试代码:

HTML

<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myApp">
<head>
    <script src="angular.js"></script>
    <script src="code.js"></script>
</head>
<body>
    <div ng-controller="testCtrl">
        <form>
            <select ng-model="year" ng-options="y for y in years">
            </select>
        </form>
    </div>
</body>
</html>

的js

angular
.module("myApp", [])
.controller('testCtrl', function ($scope, $http, $timeout)
{
    $scope.years = [0, 1, 2];
    $timeout(function ()
    {
        $scope.year = 0;
    }, 0);
});

由于某种原因,下拉列表不会呈现所选的0.而是默认为某个创建的空选项。 但是,如果我使用$scope.year = 1;,则下拉列表会正确显示选项1。

我怀疑它与JS有关,但是我需要解决这个问题。

请注意,如果我添加一个空的选项,例如<option value=""></option>,那么就可以解决问题。

但是,这不是一个好方法,因为我不希望用户能够选择空值

0 个答案:

没有答案
相关问题