[英]AngularJS : ng-model doesn't update correctly with ng-keydown. ng-model off by one character
我正在查看AngularJS上由用戶更改的文本輸入。
但是,似乎當我從我的.js打印到控制台時,ng-model關閉了一個字符,因為更新一次按下一個鍵似乎有些滯后。
例如:如果用戶鍵入“ hello”,則我會看到“ hell”,直到觸發了另一個按鍵,這會將我看到的內容更新為“ hello”,但用戶輸入可能是“ helloW”
如果這樣的話...對不起。
基本上,有沒有一種方法可以強制從控制器更新ng模型,以便每次按鍵時都能看到用戶輸入?
抱歉,我無法發布代碼。
將ng-change
與ng-model
var myApp = angular.module('app',[]); myApp.controller('Main', function ($scope) { $scope.print = function(){ console.log($scope.myname); } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body ng-app="app" ng-controller="Main"> <input type="text" name="inputbox" ng-change="print()" ng-model="myname"> </body>
嘗試將“ ng-keyup ”與“ ng-model ”一起使用,而不是ng-keydown :)
這里的文檔: https : //docs.angularjs.org/api/ng/directive/ngKeyup
var app = angular.module('app',[]); app.controller('MyController', function ($scope) { $scope.onKeyUpPrint = function(){ // You can name the function as you want // scope will be triggered exactly on key Up console.log($scope.inputModel); } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body ng-app="app" ng-controller="MyController"> <h1>Use ng-keyup<h1> <input type="text" name="inputbox" ng-keyup="onKeyUpPrint()" ng-model="inputModel"> </body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.