我想让用户只输入一个芯片在垫-芯片与自动完成。我知道md芯片有max芯片来设置最大数量的芯片。但是我使用的是垫芯片,我怎样才能设置这样的芯片的最大数量?
<mat-chip-list maxLength="1">
:
:
</mat-chip-list>发布于 2019-07-22 10:39:16
据我所知,您不能直接在MatChipList上设置它。您可以做的是,检查每个用户试图添加的芯片的选择芯片数量,然后根据已经添加的芯片数量来决定是否添加芯片。
查看从官方文档中提取并修改以解决问题的这堆栈闪电战。
我所改变的只是这个(当通过自动完成添加芯片时):
selected(event: MatAutocompleteSelectedEvent): void {
if (this.fruits.length < 1) { // <-- THIS
this.fruits.push(event.option.viewValue);
this.fruitInput.nativeElement.value = '';
this.fruitCtrl.setValue(null);
}
}这(当通过打字添加芯片时):
add(event: MatChipInputEvent): void {
// Add fruit only when MatAutocomplete is not open
// To make sure this does not conflict with OptionSelected Event
if (!this.matAutocomplete.isOpen) {
const input = event.input;
const value = event.value;
// Add our fruit
if ((value || '').trim() && this.fruits.length < 1) { // <-- THIS
this.fruits.push(value.trim());
}
// Reset the input value
if (input) {
input.value = '';
}
this.fruitCtrl.setValue(null);
}
}基本上,在添加任何新芯片之前,您需要检查已经选择的芯片数量。相应地编辑您的代码,上面的示例应该涵盖这两种情况(自动完成和键入)。
发布于 2022-06-10 14:29:50
我有250个字符的验证芯片计数
加上我做了..。演示
const len = this.fruits.join('');
// Add our fruit
if ((value || '').trim() && len.length <= 250) {
this.fruits.push(value.trim());
}https://stackoverflow.com/questions/57138568
复制相似问题