简体   繁体   English

如何通过@input 属性应用样式,我想在 angular 中增加 mat-autocomplete 的宽度

[英]How to apply style through @input property, I want to increase width of mat-autocomplete in angular

One of friend rightly pointed out to use property @input as shown in enter link description here .一位朋友正确地指出要使用属性@input,如在此处输入链接描述中所示。 I want to apply new style and change the width of mat-autocomplete .So drop down width is still big.我想应用新样式并更改mat-autocomplete的宽度。所以下拉宽度仍然很大。

I am new Angular, I don't know how to use this property, but here is what I tried this is not working.我是新的 Angular,我不知道如何使用这个属性,但这是我尝试过的,这是行不通的。

compoenent.ts组件.ts

 @Input() panelWidth: string | number;// I tried initializing this here for ex:50px, gives error

tried applying property as attribute to mat-autocomplete as shown below尝试将属性作为属性应用到 mat-autocomplete,如下所示

 <mat-autocomplete panelWidth ="170px" #auto="matAutocomplete" [displayWith] = "displayFn">

Here is the whole template.html code snippet which does autocomplete这是整个模板。html 代码片段自动完成

  <mat-form-field >
        <input matInput [matAutocomplete]="auto"  [formControl]="customerFilterControl">
           <mat-autocomplete panelWidth ="170px" #auto="matAutocomplete" [displayWith] = "displayFn">
             <mat-option  *ngFor="let option of (filteredOptions | async)" [value] ="option">
               {{option.name}} {{option.type}}
             </mat-option>
           </mat-autocomplete>
      </mat-form-field> 

Don't use @Input() panelWidth in component.ts, this is the definition in documentation.不要在 component.ts 中使用@Input() panelWidth ,这是文档中的定义。 As end user, you should use as as follows,作为最终用户,您应该按如下方式使用,

  1. Hard coded value硬编码值
<mat-autocomplete panelWidth ="170px" ...
  1. Using variable使用变量
// define variable in class
widthOfPanel = '170px';

use it in HTML with binding,在 HTML 中使用它并绑定,

<mat-autocomplete [panelWidth]="widthOfPanel" ...

Try this in your styles在您的 styles 中试试这个

/deep/ .mat-autocomplete-panel {
    min-width: 133px;
    max-width: 340px;//your desire width
    width: 100%;
}

Use /deep/ when you style in inner component css else don't use it if you style in globally in stlye.scss在内部component css中设置样式时使用/deep/否则,如果您在stlye.scss中全局设置样式,请不要使用它

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

相关问题 如何在其他mat-autocomplete更改时绑定mat-autocomplete? - How to bind mat-autocomplete on change of other mat-autocomplete? Angular5 - 如何检测垫子自动完成中的空字段或已删除字段? - Angular5 - How to detect empty or deleted field in mat-autocomplete? Angular 6:如何访问 mat-autocomplete 下拉列表中的所有选项值? - Angular 6: how to access ALL option values in mat-autocomplete dropdown? 如何检测在 Angular 中选择了 mat-autocomplete 选项? - How to detect mat-autocomplete option is selected in Angular? mat-autocomplete 选项无法找到属性 - mat-autocomplete options not able to find property 如何显示名称但从 mat-autocomplete select 的对象中获取某些其他属性的值? 角-角材料 - How to display a name but take the values of some other property from the object from mat-autocomplete select? Angular- Angular Material 垫子自动完成中的无限滚动 angular 11 - Infinite scroll in mat-autocomplete angular 11 Mat-autocomplete 面板应与主机输入的宽度相同,或者根据内容更宽 - Mat-autocomplete panel should be the same width as host input, or wider depending on content 如何在 formBuilder 上禁用 mat-autocomplete? - How to disable mat-autocomplete on formBuilder? Angular 9 mat-autocomplete 不能与 switchmap 运算符一起正常工作 - Angular 9 mat-autocomplete not working properly with switchmap operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM