如何在 Android 上获取 angular 中的按键事件?

How to get keypress event in angular on Android?

我们如何在 Android 的 Angular 中获取按键事件及其值? 我使用 phonegap Cordova Angular JS

 <form name="myForm">
 <input type="search" name="userName" ng-model="user"  class="search-input" style="width: 96%; margin: 6px auto 6px auto;" placeholder="Начните вводить название">
 </form>
 </div>
 </div>
 user = {{user}}

非常感谢任何帮助。

 <form name="myForm">
     <input type="search" 
            name="userName" 
            ng-model="user" 
            ng-keypress="yourMethod(user)" class="search-input" 
            style="width: 96%; margin: 6px auto 6px auto;" 
            placeholder="Начните вводить название">
 </form>

 user = {{user}}

更新:

<input type="search" name="userName" ng-model="user" ng-change="getValue(user)" class="search-input" style="width: 96%; margin: 6px auto 6px auto;" placeholder="Начните вводить название">

还有我的控制器$scope.getValue = function (calll) { alert(calll); }

<form name="myForm">
 <input type="search" name="userName" ng-keypress="getValue($event) ng-model="user"  class="search-input" style="width: 96%; margin: 6px auto 6px auto;" placeholder="Начните вводить название">
 </form>
 </div>
 </div>
 user = {{user}}


In controller :-

$scope.getValue = function (event) { console.log(event) }

ng-keypress 功能在 angularjs 中可用,

您也可以以类似的方式使用 ng-keydownng-keyup。仅供参考。

供参考, ng-keypress tutorial

在这种情况下,创建指令属性可能效果更好。

angular.module('inputDirective', [])
    .directive("mydirective", function() {
        var directive = {};
        directive.restrict = 'A';
        directive.scope = {};
        directive.link = function(scope, element, attrs, controller) {
            //read the text typed in the div
            function read() {
                var html = element.html();
            }

            //do this whenever someone starts typing
            element.bind("keyup", function() {
                scope.$apply(read);
            });
        }

        return directive;
    })

在 html 中添加属性到标签。

<input mydirective type="search" name="userName" ng-model="user"  class="search-input" style="width: 96%; margin: 6px auto 6px auto;" placeholder="Начните вводить название">