使用Chrome中的类型值进行角度选择时可能出现的错误

时间:2013-09-16 16:52:48

标签: google-chrome select angularjs

这是一个小提琴:http://jsfiddle.net/NFQFp/

HTML:

<body ng-app ng-controller="bodyCtrl">
<input autofocus="autofocus" ng-model="fname"/>
<select ng-model="state">
<option ng-repeat="st in states" value="{{st}}" ng-selected="state == st">{{st}}</option>
</select>
{{state}}
</body>

JAVASCRIPT:

function bodyCtrl($scope) {
    $scope.state = '';
    $scope.states =["AK","AL","AR","AS","AZ","CA","CO","CT","DC","DE","FL","GA","GU","HI","IA","ID","IL","IN","KS","KY","LA","MA","MD","ME","MH","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY","OH","OK","OR","PA","PR","PW","RI","SC","SD","TN","TX","UT","VA","VI","VT","WA","WI","WV","WY"]
}

如果您在选择框中键入并键入A然后键入L,则模型将保留AK作为其值。如果你键入A然后R或A然后键入Z,它可以正常工作。不知道这里发生了什么。似乎是任何结果集中的第二个不更新模型。 AL,CO,DE ......等不按预期工作。这在键入时似乎只是一个问题,从列表中选择项目会按预期更新模型。如果您选择AL,然后选择AK,然后输入AL,它将更新正常。这可能与未通过浏览器触发的事件有关吗?我正在使用Chrome,它似乎会在您输入时更新模型,而不像firefox那样在您模糊选择之前不会更新它。

1 个答案:

答案 0 :(得分:2)

不要将ng-repeat与select一起使用;使用方法:

<select ng-model="state" ng-options="st for st in states">

请参阅docs

现在按A L进入AL(你必须足够快地按下它们)。小提琴:http://jsfiddle.net/XyaR9/