我有一个选择输入和一个在更改所选值时调用的函数。当我使用键盘箭头键更改值时,模型不会更新(第一次)。
有人能告诉我为什么吗?

Here is my Fiddle

JS:

var app = angular.module('myApp',[]);

function mainController($scope) {
    $scope.people = [
        {
            id: 1,
            name: 'Julius'
        },
        {
            id: 2,
            name: 'Julius Rock'
        },
        {
            id: 3,
            name: 'Christopher'
        }
    ];

    $scope.selectionChanged = function() {
        console.log('test');
    };

}
app.controller('mainController', mainController);

HTML:
<div ng-controller="mainController">
    <!-- If you change the value with the arrows ng-change doesn't work on the first change. -->
    <select ng-model="selected" ng-options="person.name for person in people track by person.id" ng-change="selectionChanged()"></select>

    <span>{{selected}}</span>
</div>

提前致谢。

最佳答案

这是一个已知问题,事实上,仍然是一个开放的问题。有几种方法可以按照您的预期工作。您可以在此处的项目问题跟踪器上阅读相关信息: select/ngOptions fails to update model after keyboard selection 。作为解决方法,您可以在 <option> 中放置一个空的 <select> 。观察以下变化...

<select ng-model="selected" ng-options="person.name for person in people track by person.id" ng-change="selectionChanged()">
    <option value=""></option>
</select>

JSFiddle Link - 更新的演示

关于javascript - 选择中的 ng-change 指令无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33061155/

10-12 12:41
查看更多