我想在角度指令函数中做这样的事情:
app.directive("detectFocus", function ($focusTest, $location, $rootScope) {
return {
restrict: "A",
scope: {
onBlur: '&onBlur',
},
link: function (scope, elem) {
elem.on("blur", function () {
console.log("blur");
if(back button is pressed) {
$focusTest.setFocusOnBlur(false);
}
else {
$focusTest.setFocusOnBlur(true);
}
});
}
}
});
如何检测是否按下后退按钮....
答案 0 :(得分:1)
您可以保留$rootScope
变量来检查backbutton
是否被按下。
$rootScope.navigated = false;
$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
if (from.name) { $rootScope.navigated = true; }
});
然后在你的link
函数中,你可以做这样的事情
link: function(scope, elem) {
elem.on("blur", function() {
console.log("blur");
if ($rootScope.navigated) {
$focusTest.setFocusOnBlur(false);
} else {
$focusTest.setFocusOnBlur(true);
}
});
}