Javascript 在Chrome和Safari中按escape时的角度关闭模式对话框

Javascript 在Chrome和Safari中按escape时的角度关闭模式对话框,javascript,angularjs,angularjs-directive,sharepoint-2013,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.ModalDia

我将以下指令附加到一个元素,当用户按下
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。有什么建议吗?

我忘了更新答案<代码>$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);
            });
        }
    };
});

即使是处理也可能因浏览器而异。使用jQuery这样的库有助于处理所有细微差别。在这里使用JQuery是一个选项吗?如果有必要,我可以使用JQuery…如果可能的话,只是想看看它是否能与本机angular一起工作。这就是全部代码吗?我想我没有看到转义键位,这通常是密钥事件的跨浏览器问题。那么转义键位是什么?如果(e.keyCode===27)或类似的东西,您还需要删除destroy上的侦听器