[英]How do I get the Panel ElementRef of Mat Autocomplete
根據MatAutocomplete 文檔,MatAutocomplete class 上有一個panel
屬性,它將為您提供面板的 elementRef。
面板:ElementRef
包含自動完成選項的面板元素。
盡管 autocomplete.panel 始終未定義,但我正在努力使其正常工作? 我錯過了什么?
<mat-autocomplete #auto="matAutocomplete">
@ViewChild("auto") autocomplete: MatAutocomplete;
ngAfterViewInit(): void {
console.log(this.autocomplete.panel); // undefined??
}
我意識到只有當面板打開並且元素在 DOM 中時,該元素才可用。 如果你仔細想想,這是有道理的......
請參閱我更新的 stackblitz以了解我的意思。 請注意,該元素尚未添加到(opened)
事件的視圖中。 這意味着您必須添加一個 setTimeout 或以某種方式等待元素被添加到 dom 中,然后才能訪問它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.