angular - 如何在自定义 NgbDatepickerKeyboardService 中从 NgbDatepicker 获取 ElementRef?
问题描述
我有一个扩展 NgbDatepickerKeyboardService 的自定义服务,我需要在processKey
方法中获取一个 ElementRef 到 datepicker。主要思想是获取 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
}
解决方案
您不能在服务中使用 ViewChild/ViewChildren,但您可以使用纯 JS 访问 DOM。您只需声明一个“id”属性或“类”即可从服务中指出。在使用“id”属性的情况下,您可以查询如下:
document.getElementById('myDatePicker')
上面的句子将是组件中的一个示例:
this.myDatePickerViewChild.nativeElement
如果有多个控件,您可以通过以下方式指出其中的一个公共类:
document.getElementsByClassName('<<your common class name>>');
通过这种方式,您可以获得所有选择器并迭代它们以执行您想要的操作。
推荐阅读
- dart - Flutter W/zipro (2568):打开归档文件 build\app\outputs\apk\app.apk 时出错:错误:转储失败,因为没有找到 AndroidManifest.xml
- javascript - 在大量数字中搜索第一个重复元素
- mysql - MySQL + Query 返回在另一个表中具有“活动”列的所有行
- php - 无法从 php“连接被拒绝”连接到 MySQL
- asp.net-mvc - HTTP 错误 500.19,错误代码:0x80070021
- tensorflow - 如何将 LSTMCell 的变量设置为输入,而不是让它在 Tensorflow 中创建?
- css - Bootstrap 4 响应式导航栏不会提供下拉菜单
- go - golang 中 PHP 的 fopen/fread/fwrite 等价物
- javascript - 为什么 md-calendar 指令初始化为错误的日期 AngularJS?
- imacros - 设置提取 Imacros br