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

后端 未结 4 1239
渐次进展
渐次进展 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:12

    Update:

    ngKeypress, ngKeydown and ngKeyup are now part of AngularJS.

    
    
    
    
    
    

    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

    
    

    JS:

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

    Solution 2. Use $watch

    
        
    

    JS:

    $scope.$watch("mobileNumber", function(newValue, oldValue) {
        /* change noticed */
    });
    

提交回复
热议问题