在Chrome和Safari中按下转义时的角度关闭模式对话框

时间:2014-06-27 22:09:39

标签: javascript angularjs angularjs-directive sharepoint-2013

当用户按下esc键时,我将以下指令附加到关闭SharePoint 2013模式对话框的元素。

    app.directive("closeDialog", function() {
    return {
        link: function(scope, element, attrs) {
            document.onkeypress = function(e) {
                var dialog = SP.UI.ModalDialog.get_childDialog();
                if(dialog || dialog != null) {
                    SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel, null);
                }
                scope.$apply();
            };
        }
    };
});

适用于IE和Firefox,但不适用于Chrome或Safari。有什么建议吗?

1 个答案:

答案 0 :(得分:3)

我忘了更新答案。 $document就是答案。

app.directive("closeDialog", function($document) {
    function closeModalDialog(scope) {
        var dialog = SP.UI.ModalDialog.get_childDialog();
        if(dialog != null) {
            SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel, null);
        }
        scope.$apply();
    }
    return {
        link: function(scope, element, attrs) {
            $document.on("keydown", function(e) {
                closeModalDialog(scope);
            });
        }
    };
});