使用angularjs的键盘事件来增强用户体验

本文介绍了在前端开发中绑定键盘事件的两种方法:使用AngularJS内置指令和自定义指令。前者通过ng-keypress实现回车键响应,后者通过创建自定义ngEnter指令实现相同功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

绑定键盘事件(尤其注意:不能用a标签,不然会失效)

推荐button

  • 方法一:ng内置指令
    <code class="bash"><button ng-click=<span class="hljs-string" style="color: rgb(0, 136, 0);">"login()"</span> ng-keypress=<span class="hljs-string" style="color: rgb(0, 136, 0);">"todoSomething(<span class="hljs-variable" style="color: rgb(102, 0, 102);">$event</span>)"</span> class=<span class="hljs-string" style="color: rgb(0, 136, 0);">"btn btn-success btn-lg"</span> ng-disabled=<span class="hljs-string" style="color: rgb(0, 136, 0);">"loginForm.<span class="hljs-variable" style="color: rgb(102, 0, 102);">$invalid</span>"</span>>
      登录
    </button></code>
    说明:在对应的控制器中的$scope上绑定一个todoSomething方法
    <code class="php">  <span class="hljs-variable" style="color: rgb(102, 0, 102);">$scope</span>.todoSomething=<span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-variable">$event</span>)</span></span>{
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102);">$event</span>.keyCode==<span class="hljs-number" style="color: rgb(0, 102, 102);">13</span>){<span class="hljs-comment" style="color: rgb(136, 0, 0);">//回车</span>
              login();
          }
      }</code>
  • 方法二:自定义指令
    html
    <code class="scala"><button ng-click=<span class="hljs-string" style="color: rgb(0, 136, 0);">"login()"</span> ng-enter=<span class="hljs-string" style="color: rgb(0, 136, 0);">"login()"</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span>=</span><span class="hljs-string" style="color: rgb(0, 136, 0);">"btn btn-success btn-lg"</span> ng-disabled=<span class="hljs-string" style="color: rgb(0, 136, 0);">"loginForm.$invalid"</span>>
      登录
    </button></code>
    指令
    <code class="php">myApp.directive(<span class="hljs-string" style="color: rgb(0, 136, 0);">'ngEnter'</span>, <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-params" style="color: rgb(102, 0, 102);">()</span> </span>{
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-params" style="color: rgb(102, 0, 102);">(scope, element, attrs)</span> </span>{
              element.bind(<span class="hljs-string" style="color: rgb(0, 136, 0);">"keydown keypress"</span>, <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-params" style="color: rgb(102, 0, 102);">(event)</span> </span>{
                  <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (event.which === <span class="hljs-number" style="color: rgb(0, 102, 102);">13</span>) {
                      scope.<span class="hljs-variable" style="color: rgb(102, 0, 102);">$apply</span>(<span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-params" style="color: rgb(102, 0, 102);">()</span> </span>{
                          scope.<span class="hljs-variable" style="color: rgb(102, 0, 102);">$eval</span>(attrs.ngEnter);
                      });
                      event.preventDefault();
                  }
              });
          };
      });</code>
    总结:两种方法都能实现敲回车登录的功能,不过推荐指令的方式,对$scope的污染比较低

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值