8

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
        }
    }
 )
3
  • Can you able to paste your some of code reference here Commented Jul 15, 2014 at 5:36
  • Try using Angular Hotkeys : chieffancypants.github.io/angular-hotkeys Commented Jul 15, 2014 at 6:14
  • You don't need to use another library, see my answer below for why your implementation isn't working. Live demo and all. Commented Jul 15, 2014 at 6:17

3 Answers 3

12

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

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

4 Comments

you might also want to pass the $event to your getValue() and check for return(enter) key pressed (event.keyCode !=13 ) then send out the alert. plnkr.co/edit/0PO3U4wp7EtYsPgBDklb?p=preview
the return key and backspace key are the common ones for a form, the OP can add other keys which is necessary for his application. Answering to the question can be for pros, but beginners need a heads up on such things
haha :D using simple words and step-by-step explanation helps remove the sorcery part :p but i agree the simple words and step-by-step can be tedious sometimes. lets not deviate the topic :)
unable to do event.prevetDefault() plnkr.co/edit/yFxdYwIt9EfVFSQ8
3

The keypress event is fired when a key is pressed down and that key normally produces a character value (use input instead). https://developer.mozilla.org/en-US/docs/Web/Events/keypress

So neither the input field value nor the scope value(apply/digest loop etc.) will reflect the expected input value.

Solution is depending on your requirements. Here are some:

1) Use another event on the inputfield: change, keyup, ...

2) Use the $event object in your listener method:

<input ng-model="NodeId_1" type="text" ng-keypress="getValue($event)"/>

$scope.getValue = function (event) {
    console.log(event)
}

3) Create a watcher for your NodeId_1 value within your scope:

$scope.$watch('NodeId_1', function(newVal) {
    ...
});

Comments

0

The watcher function work for me, I attached my example

$scope.$watch('itemm.montoAmodificar', function (newValue) {
    $scope.fnActualizarNuevoSaldoDependencia(parseFloat(newValue));
});

The html code is the following

<input ng-model="itemm.montoAmodificar" my-focus class="form-control" type="text" placeholder="Ingrese el monto" ng-keypress="fnActualizarNuevoSaldoDependencia($event);" />

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.