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