当自动完成隐藏在滚动区域中时,Angular Material自动完成面板不会隐藏

在此处输入图片说明

我为此打开了一个错误(https://github.com/angular/components/issues/20209),我在问这个问题,看看是否有解决方法或任何人都知道的修复方法。

该问题在此演示页面上可见https://stackblitz.com/edit/angular-rczreu

The issue is due to the CDK panel (autocomplete's panel) being rendered as a separate layer in the demo, distinct from the autocomplete element. So when the autocomplete element moves out of the visible area of the scrollable element, the autocomplete does become hidden, but the panel does not due to this separate layer rendering.

In pseudo code, the angular html is as follows,

<html>
<body>
<my-app>

<my-component cdkScrollable> // this is a scrollable element, it's children should become hidden when we scroll

<autocomplete></autocomplete>
<some-other-child></some-other-child>

</my-component>

</my-app>

<div class="cdk-overlay-container">
  // ... other stuff

  <div>
    // autocomplete's panel 
  </div>
</div>

Raven

Okay, I've made a fork of your StackBlitz with the solution here: https://stackblitz.com/edit/angular-rczreu-yqyphm

Basically here are the highlights:

On your component html, I added a component ID to the formField:

<mat-form-field #formField class="example-full-width">

Then on your component ts, I added the references to the ViewChild for the Autocomplete elements (the form field and the Autocomplete panel itself):

@ViewChild(MatAutocompleteTrigger) autocomplete: MatAutocompleteTrigger;
@ViewChild("formField") autoCompleteFormField: MatFormField;

然后,我将AfterViewInit的实现添加到您的组件中,以确保视图元素已经加载,因此它不是未定义的。

ngAfterViewInit() {
    var observer = new IntersectionObserver((entries) => {
      if(!entries[0].isIntersecting)
        console.log('Element is is not in screen');
        this.autocomplete.closePanel();
    }, { threshold: [1] });

    
 observer.observe(this.autoCompleteFormField._elementRef.nativeElement);
}

因此,此代码段使用的是高效的Intersection Observer API,它可以检测元素是否在屏幕上。然后,如果元素不在屏幕上,则只需关闭自动完成面板。

https://usefulangle.com/post/113/javascript-detecting-element-visible-during-scroll#:~:text=To%20know%20wh%20the%20element%20is%20fully%20visible%20in%20viewport,height %20and%20bottom%20%3E%3D%200

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Angular Material Autocomplete:当有多个自动完成控件时,自动完成面板不会在页面滚动上折叠

来自分类Dev

自动完成激活时,css隐藏左面板

来自分类Dev

滚动面板自动隐藏在组件内部

来自分类Dev

在触摸自动完成的edittext建议时隐藏SoftKeyboard

来自分类Dev

当文本字段为空时隐藏自动完成 Tableview

来自分类Dev

避免面板自动隐藏在Firefox扩展中

来自分类Dev

自动完成Vim中的隐藏文件

来自分类Dev

jQuery UI自动完成和隐藏字段

来自分类Dev

CSS-图像隐藏在溢出区域中

来自分类Dev

如何在Material-ui中隐藏onFocus上的自动完成标签?

来自分类Dev

Angular Material通过$ http调用自动完成

来自分类Dev

自动完成滚动

来自分类Dev

更改primefaces自动完成面板宽度

来自分类Dev

jQuery Mobile面板不会隐藏

来自分类Dev

jQuery Mobile面板不会隐藏

来自分类Dev

自动隐藏ubuntu-budgie面板

来自分类Dev

如何使bash停止选项卡自动完成隐藏目录

来自分类Dev

如何使bash停止选项卡自动完成隐藏目录

来自分类Dev

ASP.NET AJAX自动完成未调用代码隐藏

来自分类Dev

R 从自动完成中隐藏包中的内部对象

来自分类Dev

当用户在bash中自动完成时,从shell隐藏make目标

来自分类Dev

Qt Android:按“完成”不会隐藏键盘

来自分类Dev

WPF 代码隐藏中的简单自动完成,带有在选择完成后触发的事件

来自分类Dev

使用Codeigniter实例化类时,Eclipse不会自动完成

来自分类Dev

使用Codeigniter实例化类时,Eclipse不会自动完成

来自分类Dev

Spyder不会自动完成局部变量

来自分类常见问题

React不会渲染自动完成功能

来自分类Dev

括号不会自动完成表格属性

来自分类Dev

括号不会自动完成表格属性

Related 相关文章

热门标签

归档