【问题标题】:How to add horizontal scroll to input field for mat-chip-list in Angular?如何在 Angular 中为 mat-chip-list 的输入字段添加水平滚动?
【发布时间】:2020-12-05 22:21:23
【问题描述】:

我最近开始学习 Angular 和 Angular Material,但我想不出一种方法来为 mat-chip-list 的扩展输入字段添加滚动条。我的输入字段是可编辑的,同时还使用自动完成列表来填充数据。我尝试过类似的答案,要求您编辑 CSS,但没有一个对我有用。我需要芯片以水平滚动显示在一行中。现在,输入字段的大小刚刚增加以容纳芯片。

我的代码sn-p:

        <!--Keyword section-->
        <mat-form-field class="ranking-chip-list">
          <mat-chip-list #chipList>
            <mat-chip *ngFor="let keyword of keywords" [selectable]="selectable" [removable]="removable"
              (removed)="remove(keyword)">
              {{keyword}}
              <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
            </mat-chip>

            <input placeholder="Type your keywords to sort resumes..." #keywordInput [formControl]="keywordCtrl"
              [matAutocomplete]="auto" [matChipInputFor]="chipList"
              [matChipInputSeparatorKeyCodes]="separatorKeysCodes" (matChipInputTokenEnd)="add($event)" />
          </mat-chip-list>
          <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
            <mat-option *ngFor="let keyword of filteredKeywords | async" [value]="keyword">
              {{keyword}}
            </mat-option>
          </mat-autocomplete>
        </mat-form-field>
        <!--Keyword section ends-->
        

如果我也应该链接 TypeScript 代码,请告诉我。

【问题讨论】:

    标签: angular scroll angular-material


    【解决方案1】:

    只需禁用芯片列表持有者的 flex-wrap

    .mat-chip-list-wrapper {
        flex-wrap: nowrap;
        overflow-x: auto;
    }
    

    请注意,如果您在组件中有一些封装 - 您需要使用一些深度选择器或写入 styles.scss,也许这就是您尝试的 CSS 解决方案不起作用的原因,因为 css 是非常明显的单行解决方案

    【讨论】:

    • 非常感谢!我发现了我的问题(除了不使用 .scss 文件)。我只需要这样做:encapsulation: ViewEncapsulation.None 覆盖子组件的样式(通过:github.com/angular/components/issues/11915 了解它)
    猜你喜欢
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 2019-08-15
    • 2021-05-13
    • 2019-04-28
    相关资源
    最近更新 更多