【问题标题】:how to add mat select in Angular input如何在角度输入中添加垫选择
【发布时间】:2020-10-06 09:06:15
【问题描述】:

我正在寻找在我的输入字段上添加 mat select 但由于某种原因,当我单击箭头时,它不起作用并且没有向我显示所有列表。

我有自动完成功能,通常当用户开始输入时会显示所有要选择的列表,但我也在尝试添加 mat-select 以便用户只需单击箭头并从列表中选择,而无需先输入内容.

我遵循了下面的 stackblitz 示例(我的不需要复选框),但我的有点不同,我也有文本输入和自动完成,所以我不知道为什么我不能再输入我的输入单击箭头时,当前代码和下拉列表均未显示。

任何建议或帮助都会受到赞赏。

https://stackblitz.com/edit/angular-material-v9-mat-select-with-mat-chip-list?file=src%2Fapp%2Fselect-multiple-example.html

    <mat-form-field class="form" appearance="fill">
        <mat-label>Search or Select a Super Tag</mat-label>

        <mat-select [formControl]="superTags" multiple>
            <mat-select-trigger>
                <mat-chip-list #chipList>
                    <div>
                        <mat-chip *ngFor="let superTag of superTags" [selectable]="selectable" [removable]="removable"
                            (removed)="remove(superTag)">
                            <img class="super-icon" src="/assets/images/icons/super-tag-icon.svg">
                            {{superTag.tag}}
                            <mat-icon matChipRemove *ngIf="removable" matTooltip="Remove a super tag">cancel</mat-icon>
                        </mat-chip>
                    </div>
                    <div>
                        <input matInput  #input [(ngModel)]="tagIn" [formControl]="tagCtrl" [matAutocomplete]="auto" [matChipInputFor]="chipList"
                            [matChipInputSeparatorKeyCodes]="separatorKeysCodes" (matChipInputTokenEnd)="addTag()">

                    </div>
                </mat-chip-list>
                <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
                    <mat-option *ngIf="isLoading" class="is-Loading">
                        <mat-spinner diameter="20"></mat-spinner>
                    </mat-option>
                    <ng-container *ngIf="!isLoading">
                        <mat-option *ngFor="let tag of filteredSuperTags | async" [value]="tag">
                            {{tag}}
                        </mat-option>
                    </ng-container>
                </mat-autocomplete>
            </mat-select-trigger>
        </mat-select>
    </mat-form-field>

【问题讨论】:

标签: html css angular angular-material html-select


【解决方案1】:

在这种情况下,我不会使用芯片列表和自动完成的组合。

我认为,您真正需要的是带有选项的自动完成功能,其中包含多选复选框。试着去做,像这样。

<mat-form-field class="example-full-width">
    <input type="text" placeholder="Select Users" aria-label="Select Users" matInput [matAutocomplete]="auto" [formControl]="userControl">
  <mat-hint>Enter text to find users by name</mat-hint>
</mat-form-field>

<mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
    <mat-option *ngFor="let user of filteredUsers | async" [value]="selectedUsers">
        <div (click)="optionClicked($event, user)">
            <mat-checkbox [checked]="user.selected" (change)="toggleSelection(user)" (click)="$event.stopPropagation()">
                {{ user.firstname }} {{ user.lastname }}
            </mat-checkbox>
        </div>
    </mat-option>
</mat-autocomplete>

完整示例:

https://stackblitz.com/edit/angular-sx79hu?embed=1&file=app/multiselect-autocomplete-example.html

【讨论】:

  • 您好,感谢您提供的示例,但我忘了提到下拉列表中我并不需要复选框,我只需要箭头图标,以便用户可以单击以显示下拉列表。我尝试使用 Mat-Select,因为这就是我现在能够显示箭头图标的方式,但我遇到的问题是我无法再输入,并且当我单击箭头图标时下拉列表未显示。谢谢
  • 另外,用户搜索并按回车键或从下拉列表中选择后,它应该创建一个芯片,所以我不确定是否可以删除芯片列表。谢谢
  • 那么你为什么不能只使用基本的 mat-autocomplete 呢? NG Docs 中有很好的例子。无论如何,您可以根据需要自定义垫子自动完成功能。在 元素中添加 (keyup.enter)="enterPressedMethod()" 以确定输入后调用的组件函数。您还可以添加自定义占位符,或在自动完成输入旁边添加自定义箭头图标,并设置 (click)="arrowClicked()" 函数,您将在其中制作类似 Autocomplete.Focus 的内容
  • 我对 Angular 非常陌生,并且正在使用现有的代码库,所以我还是有点迷茫。你能告诉我使用我的代码吗?如果您能帮我在右侧添加箭头图标以及输入和下拉列表,我将不胜感激。谢谢
猜你喜欢
  • 2019-06-18
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-26
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
相关资源
最近更新 更多