bharat
bharat

Reputation: 330

ng-keypress not triggering function

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

Upvotes: 4

Views: 15535

Answers (3)

Eassa Nassar
Eassa Nassar

Reputation: 1210

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 )

Upvotes: 5

harishr
harishr

Reputation: 18055

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

Upvotes: 5

Edminsson
Edminsson

Reputation: 2506

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

Upvotes: 1

Related Questions