简体   繁体   English

Angular 材料可重复使用 mat-chip-list 与 mat-autocomplete

[英]Angular material reusable mat-chip-list with mat-autocomplete

I am trying to do something similar to this question by creating a reusable chip list form control, however I need to add an autocomplete to the chip list as well.我试图通过创建一个可重复使用的芯片列表表单控件来做与这个问题类似的事情,但是我还需要在芯片列表中添加一个自动完成功能。 The problem is that the autocomplete option list overlaps and does not expand to the full width the text box.问题是自动完成选项列表重叠并且没有扩展到文本框的全宽。 When I do not put the chip list in a reusable component the autocomplete does not overlap as expected.当我没有将芯片列表放入可重复使用的组件中时,自动完成不会像预期的那样重叠。 How do I prevent the overlap?如何防止重叠?

Forked version of the code can be found here .代码的分叉版本可以在这里找到。

在此处输入图像描述

在此处输入图像描述

add focus event to input:将焦点事件添加到输入:

(focus)="addGoodStyleToMatAuto()"

add this function to ts:将此 function 添加到 ts:

addGoodStyleToMatAuto(): void {
    setTimeout(() => {
      try {
        const matAuto = document.getElementsByClassName(
          'mat-autocomplete-panel'
        )[0];
        matAuto.parentElement.classList.add('auto-class');
      } catch {}
    }, 10);
  }

add this class to style.css:将此 class 添加到 style.css:

.auto-class {
  width: 100% !important;
  top: 120px !important;
  left: 0 !important;
  transition: 0.2s;
}

Forked version of the code can be found here .代码的分叉版本可以在这里找到。

I was able to fix the issue by using the matAutocompleteConnectedTo property on the autocomplete input which I pass to the component as an input.我能够通过在自动完成输入上使用matAutocompleteConnectedTo属性来解决这个问题,我将它作为输入传递给组件。 I connected the autocomplete to the <mat-form-field> element.我将自动完成功能连接到<mat-form-field>元素。 However, the <mat-form-field> has padding on the bottom so I added a custom class to my autocomplete with a negative margin to counteract the padding.但是, <mat-form-field>在底部有填充,所以我在自动完成中添加了一个自定义 class 并带有负边距以抵消填充。

styles.css: styles.css:

.email-autocomplete {
  margin-top: -1.34375em;
}

Forked stackblitz here 在这里分叉堆栈闪电战

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

相关问题 Angular Material 6 Mat-Chip-List-两个Mat-chip-list声明共享同一数据源 - Angular Material 6 Mat-Chip-List - two mat-chip-list declarations share the same data source Angular 2+ material mat-chip-list formArray 验证 - Angular 2+ material mat-chip-list formArray validation 输入未显示验证错误的角形材料垫芯片列表 - Angular material mat-chip-list with input not showing validation error angular 材料 mat-select with mat-chip-list 通过 ENTER 而不是 DELETE 或 BACKSPACE 删除芯片 - angular material mat-select with mat-chip-list remove chip by ENTER instead of DELETE or BACKSPACE Angular mat-chips with mat-autocomplete and input组合,从列表中选择时增加一个额外的芯片 - Angular mat-chips with mat-autocomplete and input combined, adds an extra chip when selecting from list 角材料简单垫自动完成不起作用 - Angular Material simple mat-autocomplete not working 角材垫-自动完成打开面板 - angular material mat-autocomplete opens panel angular 2 材料 2 中的预填充垫自动完成 - prefill mat-autocomplete in angular 2 material 2 Angular 材料(如果块的高度很大,则切割 mat-chip-list )。 Android上的唯一 - Angular Material ( mat-chip-list cutted if height of block is big ). The only on Android 垫芯片列表中的角材料ngFor-如何防止断线? - Angular Material ngFor in mat-chip-list - how to prevent from line-break?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM