Angular 角度材质选择:如何显示选项列表中未包含的选项?
以反应形式给出以下设置:Angular 角度材质选择:如何显示选项列表中未包含的选项?,angular,angular-material,angular-forms,Angular,Angular Material,Angular Forms,以反应形式给出以下设置: <mat-form-field> <mat-select [formControl]="selectFormCtrl"> <!-- Option search field --> <ngx-mat-select-search *ngIf="canFilter" [formControl]="optionsFilter"></ngx-mat-select-search>
<mat-form-field>
<mat-select [formControl]="selectFormCtrl">
<!-- Option search field -->
<ngx-mat-select-search *ngIf="canFilter" [formControl]="optionsFilter"></ngx-mat-select-search>
<mat-option *ngFor="let option of filteredOptions | async | slice:0:MAX_ITEMS" [value]="option.value">
{{ option.text }}
</mat-option>
<mat-option *ngIf="showNothingFound()" [value]="" disabled>Nothing found</mat-option>
</mat-select>
</mat-form-field>
问题:我想截断列表中的切片:0:MAX_项。例如,长列表(如国家/地区列表)用户可能更愿意在搜索字段中键入以过滤选项。但是如果有一个选项集没有出现在列表中,组件将不会显示该选项
想法:确保所选选项始终包含在filteredOptions中
是否有更优雅/简单的解决方案来实际设置MatSelect中的选定选项,以便在任何情况下都显示值?您可以创建一个可观察的过滤、切片选项: FilteredoptionsLiced$=组合测试 这是一种过滤方法, this.selectFormControl.valueChanges.pipestartWithnull .pipemapfilteredOptions,选择选项=>{ const slicedOptions=filteredOptions.slice0,最大项目数; 如果选择了选项&&!slicedOptions.findslicedOption=>slicedOption.value===selectedOption.value{ slicedOptions.pushselectedOption; } 返回切片掺杂; } 并在没有切片管道的模板中使用它:
我倾向于使用mat autocomplete,这样您就可以轻松地将输入字段的值设置为任何值,即使它没有出现在列表中。如何调整filteredOptions的计算方式并显式地包括所选选项?你可以听任何选择的改变来更新过滤选项thx-我最终得到了一个类似的解决方案。基本上确保所选选项始终包含在列表中。