1

当用户按下esc键时,我有以下指令附加到关闭SharePoint 2013模式对话框的元素。在Chrome和Safari中按下转义时的角度关闭模式对话框

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。有什么建议么?

+0

即使在不同的浏览器中处理也不尽相同。使用像jQuery这样的库有助于处理所有的细微差别。在这里使用JQuery的一个选项? – CarlosCervantes

+0

我可以使用jquery,如果我必须...只是试图看看它是否会与原生角度,如果可能的话。 – Mark

+1

是整个代码?我不认为我看到了转义关键位,这通常是关键事件的跨浏览器问题 – Jorg

回答

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

你也需要删除监听器 –

相关问题