2017-02-02 51 views
0

我有一个应该捕获keyup事件的谷歌地图。我的组件是Google地图div的父级。它适用于Chrome,它不适用于Firefox或IE(未经测试wtith Edge)。Keyup事件没有被Angular 2捕获@HostListener

我的组件:

@Component({ 
    selector: 'dashboard', 
    templateUrl: 'dashboard.component.html', 
    styleUrls: ['dashboard.component.css'], 
    host: { 
    '[attr.tabindex]': '-1' 
    }, 
    providers: [] 
}) 
export class DashboardComponent implements OnInit{ 

    /* some functions and other stuff here 
    * ... 
    */ 

    @HostListener('keyup', ['$event']) keyup(e) { 
    console.log('This will be called with Chrome, not with the others.'); 
    } 
}; 

你所经历的一样吗? (告诉我你是否需要更多信息)。 感谢

[编辑]
我试着用ElementRef和设置我的事件处理程序elementRef.nativeElementonkeyup财产捕捉keyup事件,没有运气

回答

0

出于某种原因,铬propogates KeyUp事件给其他浏览器的父母。您可以尝试直接订阅地图的活动。

@ViewChild('myMap') 
map: ElementRef; 

constructor(private renderer: Renderer) {}; 

ngOnInit() { 
    this.renderer.listen(this.map.nativeElement, 'keyup', (event) => { 
     // ... 
    }); 
} 

,并在模板:

<div #myMap>...</div> 
+0

不幸的是,这是行不通的。我加了'tabindex =“ - 1”'。它仍然适用于Chrome。 –

3

尝试听window:keyupdocument:keyup事件,而不是简单地keyup

Source

相关问题