AngularJs Ng-Keypress event working wrongly?

angularjs, javascript

Solution

You'll want to use `ng-keyup` instead.

`ng-keypress` happens as the key is pressed, and BEFORE the value populates the input. This is why you're not getting any value on the first keypress, but on subsequent presses you will.

Use `ng-keyup` and your problem is solved, as it happens once the value has already populated the input.

<input ng-model="NodeId_1" type="text" ng-keyup="getValue()" />

`ng-keypress` is working as intended, but it is not the directive applicable to your requirements.

Working plunkr: http://plnkr.co/edit/OHWDZo68siDlcrXnLyzJ?p=preview

Problem

I am developing an application in Angularjs. I am using ng-keypress event in input type=text. While typing value in text I'm getting wrong values in the keypress function. For example, the first time if I type "1" I am getting `undefined`. Second time, typing any other value gives the first value `<input ng-model="NodeId_1" type="text" ng-keypress="getValue()"/>` ``` var angularapp = angular.module('nameapp', []); angularapp.controller('NameCtrl', function ($scope) { $scope.getValue = function () { alert($scope.NodeId_1);//Here first time undefined is coming and second what ever we enter first value will come } } ) ```

Original source