search

Home  >  Q&A  >  body text

angular.js - There are both angular and jquery plugins to handle keydown events

I downloaded a jq plug-in online, and the input box is bound to the click event, but the angular script I wrote myself also uses ng-keydown on the same input box. . . I thought Angular's keydown processing function would be executed, but it didn't work. . . But I can't remove the click event handler of the plug-in because its function needs to be used. . . Don’t know how to deal with this situation

漂亮男人漂亮男人2816 days ago683

reply all(2)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-15 17:10:07

    <body ng-app="myApp" ng-controller="MyController">

    <!--自定义指令: 限制输入框中只能是1到100之间的数值-->
    <input type="text" score>
    <script type='text/javascript' src="angular.js"></script>
    <script type="text/javascript">

    angular.module('myApp',[])

                .directive('score', function () {
                    return {
                        link : function (scope, elements, attrs, controller) {  //在显示之前执行, 只执行一次
                            //得到input
                            //console.log('link()',elements);
                            var input = elements[0]
                            //给input绑定keyup的监听回调函数
                            input.onkeyup = function () {
                                //读取input的value, 判断是否合法
                                var score = input.value.trim()
                                //如果合法, background为white
                                if(score==='' || (score*1>=1&&score*1<=100)) {
                                    input.style.background = 'white'
                                } else {
                                    //否则为red
                                    input.style.background = 'red'
                                }
                            }
                        }
                    }
                })
                .controller('MyController', function($scope){
    
                });
                

    </script>
    </body>
    </html>

    The keyup event I just wrote today, keydown is similar

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-15 17:10:07

    You can’t write it like this,
    The first choice is require: '?ngModel',
    Then get the value of the text box ngModel.$modelValue instead of input.value,
    There is also no need for elements [0], element represents
    jquery The thinking is too serious. . .

    reply
    0
  • Cancelreply