问题描述
这是一个使用mat-autocomplete的演示应用程序,我从stackoverflow帖子中选择了在此处输入链接说明,我要显示选项的整个值.值长时.我在在此处输入链接描述时发现了类似的问题,但这并没有解决我的问题.我可以滚动并看到该值,可以修改CSS样式,以便滚动条正确可见.
This is demo app that uses mat-autocomplete, I picked from stackoverflow post enter link description here , I want to display option entire value. When the value is long. I found similar question asked enter link description here but this hasn't solved my problem. I can scroll and see the value, can modify css style so the scroll bar is properly visible.
看起来滚动条的其余部分被隐藏了!
Looks like remaining part of scroll bar is hidden!
我尝试了
.mat-option {
z-index:5000;
height:300px;
}
什么都没有解决!
最好将元素显示完整.
代码段 template.html
<mat-form-field class="example-full-width">
<input matInput placeholder="State" aria-label="State" [matAutocomplete]="auto" [formControl]="stateCtrl">
<mat-autocomplete #auto="matAutocomplete" panelWidth="320px">
<mat-option *ngFor="let state of filteredStates | async" [value]="state.name">
<img class="example-option-img" aria-hidden [src]="state.flag" height="25">
<span>{{state.name}}</span> |
<small>Population: {{state.population}}</small>
</mat-option>
</mat-autocomplete>
</mat-form-field>
typescript.ts
states: State[] = [
{
name: 'Arkansas',
population: '2.978M',
// https://commons.wikimedia.org/wiki/File:Flag_of_Arkansas.svg
flag: 'https://upload.wikimedia.org/wikipedia/commons/9/9d/Flag_of_Arkansas.svg'
},
{
name: 'California CaliforniaCaliforniaCaliforniaCaliforniaCaliforniaCaliforniaCaliforniaCaliforniaCaliforniaCalifornia',
population: '39.14M',
// https://commons.wikimedia.org/wiki/File:Flag_of_California.svg
flag: 'https://upload.wikimedia.org/wikipedia/commons/0/01/Flag_of_California.svg'
}
];
constructor() {
this.filteredStates = this.stateCtrl.valueChanges
.pipe(
startWith(''),
map(state => state ? this._filterStates(state) : this.states.slice())
);
}
private _filterStates(value: string): State[] {
const filterValue = value.toLowerCase();
return this.states.filter(state => state.name.toLowerCase().indexOf(filterValue) === 0);
}
推荐答案
您有3种解决方法
1.将滚动条添加到mat-option
,因为您已经在问题中滚动了滚动条
1.Adding scroll to mat-option
as you already have that in your question
2.将宽度赋予mat-select
,但是如果mat-option
过长,也会出现此问题.
2.Giving width to mat-select
, but this will also be issue if mat-option
is too long.
3.此选项是自动换行-在CSS下方添加到mat-option
3.This option is to wrap text - add below css to mat-option
.mat-option {
word-break: break-all;
white-space: normal;
height: unset;
}
这篇关于悬停在mat-autocomplete中时如何显示整个选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!