如何检测角度指令中是否按下了后退按钮?

时间:2017-01-16 11:47:41

标签: angularjs

我想在角度指令函数中做这样的事情:

    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);
                }
            });
        }
    }
});

如何检测是否按下后退按钮....

1 个答案:

答案 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);
        }
    });
}