防止退格在AngularJS中导航回来 [英] Prevent backspace from navigating back in AngularJS

查看:112
本文介绍了防止退格在AngularJS中导航回来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AngularJS网络应用程序中遇到了这个问题。

I faced this issue in my AngularJS webapp.

当用户输入带有填写表单的页面并开始输入时,如果他按下退格键并且焦点不在输入文本上,然后页面进入上一个状态。

When a user enters a page with a form to fill and he starts typing, if he presses the backspace key and the focus is not on the input text, then the page goes to the previous state.

我查了一下这个解决方案使用jQuery,但它似乎不适合在AngularJS中实现这一点。

I looked up this solution using jQuery, but it doesn't seem the appropiate way for achieve this in AngularJS.

推荐答案

角度js中有 $ document

angular.module('yourModule', [])
  .controller('yourController', ['$scope', '$document', function($scope, $document) {
      $document.on('keydown', function(e){
          if(e.which === 8 && ( e.target.nodeName !== "INPUT" && e.target.nodeName !== "SELECT" ) ){ // you can add others here inside brackets.
              e.preventDefault();
          }
      });

    }
  ]);



Plnkr演示。



你可以在演示中看到我只用于INPUT nodeName,它不会阻止文本输入的退格键的默认值,但不会阻止 textarea 的默认值,因为我们还没有在条件中处理它。

Plnkr Demo.

You can see in the demo i have used only for "INPUT" nodeName and it does not prevent the default of the backspace key on text input but not on textarea because we have not handled it in the condition.

这篇关于防止退格在AngularJS中导航回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆