此 Controller 适用于 page1.html
test1.controller('test1Controller', function($scope,$document) {
$document.on('keydown', function(e){
if(e.which === 8 && e.target.nodeName !== "INPUT" && e.target.nodeName !== "SELECT"){
confirm("you are In Home page");
}
});
}
此 Controller 适用于 page2.html
test2.controller('test2Controller', function($scope,$document) {
$document.on('keydown', function(e){
if(e.which === 8 && e.target.nodeName !== "INPUT" && e.target.nodeName !== "SELECT"){
//HERE CODE LOGIC SHOULD COME TO PREVIOUS PAGE
}
});
}
我在page1中输入数据并移动到page2.html并单击键盘的退格键, 虽然我在第二页,但 test1Controller 中的函数正在被调用。
上面的逻辑有什么问题..
使用当前上述代码,两个不同 Controller 中的函数都被调用。
最佳答案
在 Controller 中绑定(bind)新事件之前,请取消绑定(bind)所有 keydown 事件
对于 Controller 一
test1.controller('test1Controller', function($scope,$document) {
$document.off('keydown')
$document.on('keydown', function(e){
if(e.which === 8 && e.target.nodeName !== "INPUT" && e.target.nodeName !== "SELECT"){
confirm("you are In Home page");
}
});
}
对于 Controller 二
test2.controller('test2Controller', function($scope,$document) {
$document.off('keydown')
$document.on('keydown', function(e){
if(e.which === 8 && e.target.nodeName !== "INPUT" && e.target.nodeName !== "SELECT"){
//HERE CODE LOGIC SHOULD COME TO PREVIOUS PAGE
}
});
}
关于javascript - 不同 Controller 中不同的后退空间处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38786485/