javascript - 防止退格键在 AngularJS 中导航回来

标签 javascript jquery angularjs backspace

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

当用户进入一个需要填写表单的页面并开始打字时,如果他按下退格键并且焦点不在输入文本上,那么页面会回到之前的状态。

我查了一下this solution使用 jQuery,但它似乎不是在 AngularJS 中实现此目的的合适方法。

最佳答案

angular 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 Demo.

你可以在演示中看到我只使用了 "INPUT" nodeName 并且它不会阻止默认的退格键在文本输入上而不是在 textarea因为我们没有在条件下处理它。

关于javascript - 防止退格键在 AngularJS 中导航回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29006000/

相关文章:

javascript - HTML 选择器多个,选项 "all"禁用所有其他选项

javascript - AngularJS 全局日期时区偏移量

javascript - 带有 ng-include 的 Angular JS ng-switch?

javascript 变量在 Highchart 中不起作用

javascript - 为什么我的按钮会增加错误的变量?

angularjs - 使 Angular 更容易升级所需的绝对最低限度的测试

javascript - 如何在 Angular 中唯一标识组件的父组件

php - 使用 vTicker JQuery 时 $ 不是函数错误

javascript - 使用 Lodash 比较、匹配和过滤两个对象

javascript - 使用 jQuery 在 mousedown 函数中绑定(bind) mousemove