简体   繁体   English

输入字段未随着ng-keydown事件更新

[英]Input field not updating with ng-keydown event

In this piece of code I want to add validation to input field, if the value is 0 but I don't know why I am not able to update and enter new value in the input field. 在这段代码中,如果值是0,但我想向输入字段添加验证,但我不知道为什么我无法更新并在输入字段中输入新值。 Simply I am not able to change the input field value. 简而言之,我无法更改输入字段的值。

Value remain same if I delete existing value and add something in existing value. 如果删除现有值并在现有值中添加一些内容,则值保持不变。

Here is HTML code: 这是HTML代码:

<input ng-model="lineitemDetailsCtrlAs.lineItems.orderedQuantity" type="text" class="col-md-6 text-right panel-row-spacing"
                ng-keydown="valueChanged($event)" required
                />

and angular code is: 角度代码是:

$scope.valueChanged = function (event) {
 var quantityRequire={};
    if (event.keyCode === 48 || lineitemDetailsCtrlAs.lineItems.orderedQuantity == 0) {
            quantityRequire = {
              "messageId": "ORDERED_QUANTITY",
              "params": [],
              "severity": "ERROR"
            };
            lineitemDetailsCtrlAs.errorMessages.push(quantityRequire);
          }
          else{
            event.preventDefault();
          }
    };

您将通过“ event.preventDefault();”来停止事件,因为只有键码48(仅数字0)是可以接受的,其他键事件在其他情况下会下降并停止更新输入值。

I think Rameshkumar Arumugam is right about what's wrong with your code, below is a working example 我认为Rameshkumar Arumugam对您的代码出了问题是正确的,下面是一个有效的示例

 angular.module("myApp", []) .controller("MainCtrl", function($scope) { $scope.lineItems = { orderedQuantity: 12 }; $scope.errorMessages = []; $scope.valueChanged = function(event) { var quantityRequire = {}; if (event.keyCode === 48 || $scope.lineItems.orderedQuantity == 0) { quantityRequire = { "messageId": "ORDERED_QUANTITY", "params": [], "severity": "ERROR" }; alert(quantityRequire["messageId"]); $scope.errorMessages.push(quantityRequire); } }; }) 
 <div ng-app="myApp"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <div ng-controller="MainCtrl"> <input ng-model="lineitemDetailsCtrlAs.lineItems.orderedQuantity" type="text" class="col-md-6 text-right panel-row-spacing" ng-keydown="valueChanged($event)" required /> </div> </div> 

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

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