3

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.

Kailas
  • 7,350
  • 3
  • 47
  • 63
Maksim Morozov
  • 191
  • 3
  • 11

4 Answers4

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

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

jezmck
  • 1,138
  • 3
  • 18
  • 38
Ved
  • 11,837
  • 5
  • 42
  • 60
  • ng-change="yourMethod(user)". – Ved Jan 23 '15 at 06:09
  • This is my input And my controller $scope.getValue = function (calll) { alert(calll); } but i see alert UNDEFINED – Maksim Morozov Jan 23 '15 at 06:11
  • ng-model binds your view to controller.. ng-model="user" means the value you type in input box is the in user .. – Ved Jan 23 '15 at 06:12
  • I am facing same issue on android, tried `ng-change` but didn't work for me. https://stackoverflow.com/q/58113528/3081929 – iBug Sep 27 '19 at 08:30
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) }
squiroid
  • 13,809
  • 6
  • 47
  • 67
0

ng-keypress function available in angularjs,

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

For reference , ng-keypress tutorial

0

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="Начните вводить название">
jsbisht
  • 9,079
  • 7
  • 50
  • 55