简体   繁体   English

如何覆盖角度4中的md-autocomplete scss?

[英]How to override md-autocomplete scss in angular 4?

I have created an app where I have used md-autocomplete from material.angular.io. 我创建了一个应用程序,我使用了material.angular.io中的md-autocomplete。

Now problem comes when I am trying to search anything , the dropdown of suggestions has less width. 现在当我尝试搜索任何内容时出现问题,建议的下拉列表宽度较小。 So I want to increase the width of md-autocomplete. 所以我想增加md-autocomplete的宽度。

I have tried overriding scss, inline scss but no use. 我试过重写scss,内联scss但没有用。

Below is my code. 以下是我的代码。

<div class="fill-remaining-space">
  <md-input-container class="example-full-width" color="accent">
    <md-icon mdPrefix>search</md-icon>
    <input type="text" mdInput [mdAutocomplete]="auto" [formControl]="searchCtrl">
    <md-icon mdSuffix (click)="clear()">clear</md-icon>
  </md-input-container>
  <md-autocomplete #auto="mdAutocomplete" id="autocomplete">
    <md-option *ngFor="let result of filteredResults" [value]="result.name">
      <span [routerLink]="['/category',result.type]" class="row"> {{result.name | lowercase}}<span>&nbsp; in &nbsp;<span class="type"> {{result.type}} </span></span>
      </span>
    </md-option>
  </md-autocomplete>
</div>

I am trying to override this scss using: 我试图使用以下方法覆盖此scss:

.fill-remaining-space {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 9px;
    margin-left: 14%;
    width: 74%;
    .mat-autocomplete-panel.mat-autocomplete-visible /deep/ {
      background-color: blue !important;
    }
  }

Is there anything wrong I am doing? 我在做什么问题? Thanks 谢谢

The proper way to do it is to set the View Encapsulation to None on your component: 正确的方法是在组件上将View Encapsulation设置为None:

@Component({
    templateUrl: './my.component.html' ,
    styleUrls: ['./my.component.css'], 
    encapsulation: ViewEncapsulation.None,
})

Then in your component css you can do exactly what you tried: 然后在您的组件css中,您可以完全按照您的尝试进行操作:

.mat-autocomplete-panel.mat-autocomplete-visible {
      background-color: blue !important;
    }

View Encapsulation = None means that Angular does no view encapsulation. View Encapsulation = None表示Angular不进行视图封装。 Angular adds the CSS to the global styles. Angular将CSS添加到全局样式中。 The scoping rules, isolations, and protections discussed earlier don't apply. 前面讨论的范围规则,隔离和保护不适用。 This is essentially the same as pasting the component's styles into the HTML. 这与将组件的样式粘贴到HTML中基本相同。

I just added this to the global styles: 我刚刚将它添加到全局样式中:

.mat-autocomplete-panel {
    width: 30rem;
}

See plunker (the global styles are here the style tags in index.html) 请参阅plunker (全局样式在这里是index.html中的样式标记)

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

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