angular - 如何将 Angular Material 多选项目限制为 N 个项目?
问题描述
在使用https://material.angular.io/components/select/overview#multiple-selection
如何将选择的项目数限制为 N 个?其中 N 为 3 或 4 或 5。
解决方案
在组件上设置selectionChange
输出事件,将mat-select
其指向您的组件函数:(selectionChange)="changed()"
.
片段:
<mat-select placeholder="Toppings" [formControl]="toppings" (selectionChange)="changed()" multiple>
在您的组件中创建一个名为mySelections
. 这将存储您的选择:) 它将包含一个字符串数组。
它看起来像这样:
mySelections: string[];
changed() {
if (this.toppings.value.length < 3) {
this.mySelections = this.toppings.value;
} else {
this.toppings.setValue(this.mySelections);
}
}
将数字 3 更改为 N,然后就完成了。
推荐阅读
- html - 如何检查任何元素是否在jQuery中有一个类?
- javascript - body.req 被拆分为多个日志
- windows - 为什么wsl2 wslconfig文件不起作用?
- java - 如何创建一种方法来生成所有可能的布尔函数?
- swift - swift中的标注按钮和firestore出现问题
- rx-java - RxJava - 在 map 和 doOnSuccess 中执行长时间运行任务的任何问题
- php - PHP | 未在数据库中加载 CSV 数据
- html - HTML的value属性如何标签工作?
- firebase - Firebase 翻译文本扩展 - 是否可以翻译嵌套集合中的字段?
- java - 我在没有“catch”、“finally”或资源声明的情况下遇到错误,我该如何在下面的代码中解决这个问题