【问题标题】:Angular does not get javascript onscreen keyboard inputAngular 没有获得 javascript 屏幕键盘输入
【发布时间】:2014-09-27 01:35:48
【问题描述】:

我正在处理一个页面,该页面应该有一个输入字段和一个屏幕键盘,用 js 实现。我用过这个键盘:http://jabtunes.com/notation/keyboardcanvasexamples.html

输入字段得到输入就好了,问题是依赖于这个输入字段的角度过滤器不起作用。我在这个 plunker 中发现了一个类似的问题,但没有解决方案: http://plnkr.co/edit/FnrZTAwisYub5Vukaw2l?p=preview

html:

<html ng-app="plunker">
  <head>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.min.js"></script>
    <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.6.0.js"></script>
    <script src="example.js"></script>
    <script src="jKeyboard.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

<div class='container-fluid' ng-controller="TypeaheadCtrl">
    <pre>Model: {{selected| json}}</pre>
    <input type="text" ng-model="selected" id="testInput" typeahead="state.name for state in states | filter:$viewValue | limitTo:8">
</div>
    <button id="btn">E</button>
  </body>
</html>

js:

angular.module('plunker', ['ui.bootstrap']);
function TypeaheadCtrl($scope) {

  $scope.selected = undefined;

$scope.WatchPrintList = function () {
        $scope.selected= {};
        $scope.$watch('#testInput', function(newVal, oldVal) {
            $scope.selected = newVal;
        }, true);
    }

  $scope.states = [
{"name":"Alabama","alpha-2":"AL"},
{"name":"Alaska","alpha-2":"AK"},
//etc etc
];
}

使用屏幕键盘键入时,没有过滤器响应,但使用真实键盘键入时,它们会得到更新,数据也会被相应地过滤。为什么?

感谢您的帮助!

【问题讨论】:

  • 正如@user3651406 所指出的,问题在于 jKeyboard.js 不能模拟真实的键盘事件。您需要自己修改 jKeyboard,至少要触发“输入”事件。或者找到另一个更强大的库。

标签: javascript html angularjs keyboard on-screen-keyboard


【解决方案1】:

简答:
我认为 Angular 不知道此处有任何 $scope 更改(单击屏幕键盘时)。

这是为什么呢?
免责声明:我也是 AngularJS 的新手。所以我的解释在某些方面可能是错误的。 然而,第一次分析向我表明,您的 jkeyboard.js 似乎直接操纵了内容。它不会模拟真实的键盘,因为它不会分别触发 keydown 事件或 keypress。 我还查看了 angular-ui 的 typeahead 指令。在这里,他们至少会监听一些 keydown 事件(虽然不完全):

//bind keyboard events: arrows up(38) / down(40), enter(13) and tab(9), esc(27)

(见https://github.com/angular-ui/bootstrap/blob/master/src/typeahead/typeahead.js#L268

仅此一个问题肯定会导致兼容性问题。

你能做些什么?
可能自己编写一个指令,负责修补您的 jkeyboard.js,以触发适当的事件和/或在正确的时刻调用 $scope.$apply()。

希望我能以某种方式提供帮助!

【讨论】:

  • 我认为$scope.$apply() 不会有帮助。但是,是的,你是对的,这个问题的关键是 jKeyboard 没有模仿真正的键盘,即不触发任何事件。
  • 好的,谢谢你的建议!你们是否认为有一些简单的方法可以以某种方式更改 js 键盘,使其模仿真实的键盘 - 例如触发 keydown 事件等?
猜你喜欢
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 2012-09-21
  • 2011-01-25
  • 1970-01-01
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多