How can I detect keydown or keypress event in angular.js?

后端 未结 4 1230
渐次进展
渐次进展 2020-12-23 20:36

I\'m trying to get the value of a mobile number textbox to validate its input value using angular.js. I\'m a newbie in using angular.js and not so sure how to implement thos

相关标签:
4条回答
  • 2020-12-23 21:01

    You were on the right track with your "ng-keydown" attribute on the input, but you missed a simple step. Just because you put the ng-keydown attribute there, doesn't mean angular knows what to do with it. That's where "directives" come into play. You used the attribute correctly, but you now need to write a directive that will tell angular what to do when it sees that attribute on an html element.

    The following is an example of how you would do that. We'll rename the directive from ng-keydown to on-keydown (to avoid breaking the "best practice" found here):

    var mod = angular.module('mydirectives');
    mod.directive('onKeydown', function() {
        return {
            restrict: 'A',
            link: function(scope, elem, attrs) {
                 // this next line will convert the string
                 // function name into an actual function
                 var functionToCall = scope.$eval(attrs.ngKeydown);
                 elem.on('keydown', function(e){
                      // on the keydown event, call my function
                      // and pass it the keycode of the key
                      // that was pressed
                      // ex: if ENTER was pressed, e.which == 13
                      functionToCall(e.which);
                 });
            }
        };
    });
    

    The directive simple tells angular that when it sees an HTML attribute called "ng-keydown", it should listen to the element that has that attribute and call whatever function is passed to it. In the html you would have the following:

    <input type="text" on-keydown="onKeydown">
    

    And then in your controller (just like you already had), you would add a function to your controller's scope that is called "onKeydown", like so:

    $scope.onKeydown = function(keycode){
        // do something with the keycode
    }
    

    Hopefully that helps either you or someone else who wants to know

    0 讨论(0)
  • 2020-12-23 21:11

    JavaScript code using ng-controller:

    $scope.checkkey = function (event) {
      alert(event.keyCode);  //this will show the ASCII value of the key pressed
    

    }

    In HTML:

    <input type="text" ng-keypress="checkkey($event)" />
    

    You can now place your checks and other conditions using the keyCode method.

    0 讨论(0)
  • 2020-12-23 21:12

    Update:

    ngKeypress, ngKeydown and ngKeyup are now part of AngularJS.

    <!-- you can, for example, specify an expression to evaluate -->
    <input ng-keypress="count = count + 1" ng-init="count=0">
    
    <!-- or call a controller/directive method and pass $event as parameter.
         With access to $event you can now do stuff like 
         finding which key was pressed -->
    <input ng-keypress="changed($event)">
    

    Read more here:

    https://docs.angularjs.org/api/ng/directive/ngKeypress https://docs.angularjs.org/api/ng/directive/ngKeydown https://docs.angularjs.org/api/ng/directive/ngKeyup

    Earlier solutions:

    Solution 1: Use ng-change with ng-model

    <input type="text" placeholder="+639178983214" ng-model="mobileNumber" 
    ng-controller="RegisterDataController" ng-change="keydown()">
    

    JS:

    function RegisterDataController($scope) {       
       $scope.keydown = function() {
            /* validate $scope.mobileNumber here*/
       };
    }
    

    Solution 2. Use $watch

    <input type="text" placeholder="+639178983214" ng-model="mobileNumber" 
    ng-controller="RegisterDataController">
        
    

    JS:

    $scope.$watch("mobileNumber", function(newValue, oldValue) {
        /* change noticed */
    });
    
    0 讨论(0)
  • 2020-12-23 21:19

    You can checkout Angular UI @ http://angular-ui.github.io/ui-utils/ which provide details event handle callback function for detecting keydown,keyup,keypress (also Enter key, backspace key, alter key ,control key)

    <textarea ui-keydown="{27:'keydownCallback($event)'}"></textarea>
    <textarea ui-keypress="{13:'keypressCallback($event)'}"></textarea>
    <textarea ui-keydown="{'enter alt-space':'keypressCallback($event)'}"> </textarea>
    <textarea ui-keyup="{'enter':'keypressCallback($event)'}"> </textarea>
    
    0 讨论(0)
提交回复
热议问题