7

我创建了 js 小提琴:小提琴

我创建了一个包含一些 ng-options 的表单,当您使用按钮而不是鼠标时它会出现奇怪的行为(只需单击文本框并按“tab”,您可以使用箭头键选择它)。

<form ng-controller="MyApp" id="Apps" name="Apps" ng-submit="SendApp()" role="form" novalidate>
    <input type="text" name="title" ng-model="Info.Title" />
    <select id="Formula" ng-model ="Info.Genre" ng-change= "ChangeGenre()"
                        ng-options="id as name for (id, name) in Genre" blank></select>
     <select class="form-control" ng-model ="Info.Program" 
                        ng-options="Program as Name for (Program, Name) in Program" ng-change="ChangeProgram()" blank></select>
    <h3>{{Info.Genre}}</h3>
    <h3>{{Info.Program}}</h3>
    <button type=submit>Submit this </button>
</form>

Javascript:

var theApp = angular.module("TheApp", []);

theApp.controller("MyApp", ['$scope', function($scope){
    $scope.Program = {"1":"Music","2":"Theater","3":"Comedy"};

    $scope.Genre = {"1":"Mystery", "2":"Thriller", "3":"Romance"};

    $scope.ChangeProgram = function(){
        alert($scope.Info.Program + " " + $scope.Info.Genre);
    }
    $scope.ChangeGenre = function (){
        console.log($scope.Info.Genre);

    }
    $scope.SendApp = function(){
        alert($scope.Info.Program + " " + $scope.Info.Genre);
    }
}]);

当您在 First try 中选择第一个选项时,ng-model 不会更新。

出了什么问题,以及如何解决这个问题?

Update:

正如下面评论中提到的,要重现,请在文本字段中输入鼠标,将选项卡输入到组合框,然后尝试使用键盘选择第二个选项(Thriller)。这将在第一次尝试时失败,一旦选择了第三个或第一个选项,第二个选项也会被识别。

4

1 回答 1

14

使用此处提出的指令,这对我有用:

theApp.directive("select", function() {
    return {
      restrict: "E",
      require: "?ngModel",
      scope: false,
      link: function (scope, element, attrs, ngModel) {
        if (!ngModel) {
          return;
        }
        element.bind("keyup", function() {
          element.triggerHandler("change");
        })
      }
   }
})

我把小提琴分叉了。

于 2014-03-25T11:21:08.390 回答