我已经开始研究Angular2并且有一个基本的3个嵌套组件工作.但是我无法弄清楚如何将keypress处理程序添加到文档中.
如果没有,我将如何监听文件上的按键并对其作出反应?为了清楚起见,我需要回复文档本身的问题,而不是输入.
在Angular 1中,我将创建一个指令并使用$ document; 这样的事情:
$document.on("keydown", function(event) {
// if keycode...
event.stopPropagation();
event.preventDefault();
scope.$apply(function() {
// update scope...
});
Run Code Online (Sandbox Code Playgroud)
但我还没有找到一个Angular2解决方案.
在一个组件中包含许多不同组件的应用程序中,我有一个自定义自动建议框.如果用户点击自动建议框(或包含自动建议框的元素)的任何位置,则应关闭该框.
这就是我在jQuery中要做的事情:
$(document).on('click','body',function(e) {
if(e.target!='.suggestbox' && $(e.target).parent('.suggestbox').length <1 ) {
$('.suggestbox').remove();
}
});
Run Code Online (Sandbox Code Playgroud)
但是在我的Angular Dart模板中,我有:
index.html的:
<body>
<my-app>
// sub component
// sub sub component
</my-app>
</body>
Run Code Online (Sandbox Code Playgroud)
我可以想到在my-app组件中检测到最顶层包装器上的点击并将操作发送到子组件但是这仍然不是主体点击的可能性.
解决这个问题的最佳方法是什么?