我在我的 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/