简体   繁体   English

AngularJs Ng-Keypress活动工作错误?

[英]AngularJs Ng-Keypress event working wrongly?

I am developing an application in Angularjs. 我正在Angularjs开发一个应用程序。 I am using ng-keypress event in input type=text. 我在input type = text中使用ng-keypress事件。 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 . 例如,如果我输入“1”,我第一次得到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
        }
    }
 )

You'll want to use ng-keyup instead. 你会想要使用ng-keyup

ng-keypress happens as the key is pressed, and BEFORE the value populates the input. ng-keypress时会发生ng-keypress ,并且在值填充输入之前。 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. 使用ng-keyup并解决您的问题,因为一旦值已经填充输入就会发生。

<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. ng-keypress按预期工作,但它不适用于您的要求。

Working plunkr: http://plnkr.co/edit/OHWDZo68siDlcrXnLyzJ?p=preview 工作人员: http ://plnkr.co/edit/OHWDZo68siDlcrXnLyzJ?p =preview

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 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, ... 1)在输入字段上使用另一个事件:更改,密钥,...

2) Use the $event object in your listener method: 2)在监听器方法中使用$ event对象:

<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: 3)为您的范围内的NodeId_1值创建一个观察器:

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

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 html代码如下

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM