4

I'm new to AngularJS. I'm implementing ng-keypress events in AngularJS. I referred to many blogs and tried to do as it is shown, but my code is not working!

    <div ng-app="myApp">
        <div ng-controller="MainCtrl">
            <input  ng-keypress="change($event)" type="text" >
            {{ text }}
        </div>
    </div>

script is:

    var myApp = angular.module('myApp', []);

    myApp.controller('MainCtrl', ['$scope', function ($scope) {
        $scope.change=function($event){
            $scope.text= 'a';
        };
    }]);

I'm trying to change the value of {{text}} on keypress.. but it's not working! can anyone help me out!

Thanks :)

3 Answers 3

6

sometimes ng-keypress do not work on some browsers !! i had the issue with the arrow keys in chrome try ng-keydown instead (it worked for me )

Sign up to request clarification or add additional context in comments.

2 Comments

I encountered same issue. I have implemented type head plugin of boot strap with angular. And on input I have bind key press event. When I start typing before selecting then key press event was firing but when I selected a option from typehead and again started typing key presss event stops working. It works again untill I remove the whole text from input. I am shocked why it happening. But keydown event worked perfectly. This issue is occuring only on CHROME and EDGE.
Here is the code pen link codepen.io/varinder_rai/pen/YpReBG fort issue which I have described above. When keypress event will be changed to keydown it works properly.
5

i tried the same things and its working

  <body ng-controller="MainCtrl">
    <input  ng-keypress="change($event)" type="text" >
    <pre>{{name}}</pre>
  </body>


app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.change=function($event){
    $scope.name= 'hello';
    };
});

checkout this plunker... PLUNKER LINK

EDIT checkout your code plunker.. thats also working

EDIT

finally the answer is: 1.0.7 does not support ng-keypress, hence it was not working..

3 Comments

i used minified AngularJS script from google api.. i doubted it and changed to script u included in plunker!! shockingly it's working. but why it didn't worked with this ( <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script> )
i dont think 1.0.7 had ng-keypress... 1.0.7 api
ohh.. i'm using old release it seems! thanks for your reply harish. :)
1

As everyone else says it's working as it is supposed to do. Perhaps you want something like this?

var myApp = angular.module('myApp', []);

myApp.controller('MainCtrl', ['$scope', function ($scope) {
  $scope.text = '';
  $scope.change=function($event){
    $scope.text += String.fromCharCode($event.keyCode);
  };
}]);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.