ng-keypress not triggering function

14,751

Solution 1

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

Solution 2

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 )

Solution 3

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);
  };
}]);
Share:
14,751
bharat
Author by

bharat

getting skilled day by day :)

Updated on June 27, 2022

Comments

  • bharat
    bharat about 2 years

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

  • bharat
    bharat almost 10 years
    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> )
  • harishr
    harishr almost 10 years
    i dont think 1.0.7 had ng-keypress... 1.0.7 api
  • bharat
    bharat almost 10 years
    ohh.. i'm using old release it seems! thanks for your reply harish. :)
  • Varinder
    Varinder over 7 years
    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.
  • Varinder
    Varinder over 7 years
    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.