简体   繁体   English

如何在Angular 2 Material 2中更改md-autocomplete宽度?

[英]How to change md-autocomplete width in Angular 2 Material 2?

I'm stuck at making the md-autocomplete's width grow to become the size of the relevant mdInput. 我坚持让md-autocomplete的宽度增长到相关mdInput的大小。 Here's my code: 这是我的代码:

<div class="field">
        <md-input-container class="halfWidth">
            <input mdInput 
            type="text" 
            name="country" 
            [(ngModel)]="model.Country" 
            (ngModelChange)="filterCountries()" 
            [disabled]="progress" 
            [mdAutocomplete]="countriesAutocomplete" 
            placeholder="Country" 
            required />
        </md-input-container>
        <md-autocomplete #countriesAutocomplete="mdAutocomplete" 
            [displayWith]="getCountryName">
            <md-option *ngFor="let country of countries" 
                    [value]="country" 
                    class="fullWidth">
                {{ country.PersianName || country.EnglishName }}
            </md-option>
        </md-autocomplete>
    </div>

And here's the result picture: 这是结果图: 在此输入图像描述

I used md-autocomplete and md-autocomplete .mat-autocomplete-panel CSS selectors, but no result. 我使用了md-autocompletemd-autocomplete .mat-autocomplete-panel CSS选择器,但没有结果。

I think i found the answer for this.. i would like to caution that it was a trial n error and i haven't checked the consequence of this as yet. 我想我找到了答案。我想提醒一下,这是一个试验n错误,我还没有检查这个的后果。 Please use it cautiously. 请谨慎使用。 Here is what worked for me.. i had set the max-width property to 'none' and width property to 'auto' for class .cdk-overlay-pane in my global CSS file and it seemed to have resolved the issue for me. 这对我有用..我在我的全局CSS文件中为类.cdk-overlay-pane将max-width属性设置为'none',将width属性设置为'auto',它似乎已经解决了我的问题。 Hope it helps... cheers! 希望它有所帮助...干杯! Happy coding ... :) 快乐的编码...... :)

.cdk-overlay-pane{
max-width:none !important;
width:auto !important;}

Try this. 试试这个。 If you want your component styles to cascade to all child elements of a component, but not to any other element on the page: 如果希望组件样式级联到组件的所有子元素,而不是页面上的任何其他元素:

 :host ::ng-deep .mat-autocomplete-panel{
        width:fit-content;
        }

As per my answer to a similar question, you can now use the new panelWidth property. 按我的回答对一个类似问题,你现在可以使用新的panelWidth财产。

@Input()
panelWidth: string | number

Specify the width of the autocomplete panel. 指定自动完成面板的宽度。 Can be any CSS sizing value, otherwise it will match the width of its host. 可以是任何CSS大小调整值,否则它将匹配其主机的宽度。

That doesn't work. 这不起作用。 I've tried editing all of the CSS. 我已经尝试过编辑所有的CSS。 I think they are preventing it since it's in beta still. 我认为他们正在阻止它,因为它还处于测试阶段。

I don't know of a way to do it. 我不知道有办法做到这一点。 :( :(

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM