简体   繁体   English

如何在自定义 NgbDatepickerKeyboardService 中从 NgbDatepicker 获取 ElementRef?

[英]How to get ElementRef from NgbDatepicker in custom NgbDatepickerKeyboardService?

I have a custom service that extends NgbDatepickerKeyboardService and I need to get an ElementRef to datepicker in processKey method.我有一个扩展 NgbDatepickerKeyboardService 的自定义服务,我需要在processKey方法中获取到 datepicker 的processKey The main idea is to get ElementRef and to query from this reference trying to find a div of the next day (I need to check if div has a class).主要思想是获取 ElementRef 并从该参考中查询,试图找到第二天的 div(我需要检查 div 是否有一个类)。

public processKey(event: KeyboardEvent, datepicker: NgbDatepicker): void {
    const state = datepicker.state;
    // how to get it here? NgbDatepicker does not have any public prop to do it
}

You cannot use ViewChild/ViewChildren in Services but you can access to DOM with plain JS.您不能在服务中使用 ViewChild/ViewChildren,但您可以使用纯 JS 访问 DOM。 You just have to declare an "id" attribute or "class" to point out from the service.您只需要声明一个“id”属性或“class”来从服务中指出。 In the case of utilize an "id" attribute you can query as follow:在使用“id”属性的情况下,您可以查询如下:

document.getElementById('myDatePicker')

The above sentence would be an example in a component:上面的句子将是一个组件中的示例:

this.myDatePickerViewChild.nativeElement

If there are several controls you can point out to a common class in them with:如果有多个控件,您可以使用以下命令指出其中的一个公共类:

document.getElementsByClassName('<<your common class name>>');

In that way you get all pickers and iterates them to do what you want.通过这种方式,您可以获得所有选择器并迭代它们以执行您想要的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM