如何在Android上获得角度按键事件?

时间:2015-01-23 05:28:10

标签: javascript android angularjs cordova keypress

我们如何在Android上的Angular中获取keypress事件及其价值? 我使用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}}

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:2)

 <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); }

答案 1 :(得分:0)

<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) }

答案 2 :(得分:0)

angularjs中提供

ng-keypress 功能,

您也可以类似方式使用 ng-keydown ng-keyup 。 FYI。

供参考, ng-keypress tutorial

答案 3 :(得分:0)

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

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="Начните вводить название">