How to get keypress event in angular on Android?

旧城冷巷雨未停 提交于 2019-12-12 16:25:27

问题


How do we get the keypress event and its value in Angular on Android? I Use the 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}}

Any help is really appreciated.


回答1:


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

UPDATE:

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

And my controller $scope.getValue = function (calll) { alert(calll); }




回答2:


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



回答3:


ng-keypress function available in angularjs,

You can also use ng-keydown and ng-keyup in similar manner. FYI.

For reference , ng-keypress tutorial




回答4:


Creating a directive attribute might work better in this case.

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

In the html add the attribute to the tag.

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


来源:https://stackoverflow.com/questions/28103584/how-to-get-keypress-event-in-angular-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!