Angular 我有一个下拉列表,其中有值,我只需要选择一次值,下次必须禁用该值时,
我的下拉列表有两个值或两个角色,如果我选择了一个角色,就不能再次选择相同的值。我们如何实现这一点。有人能帮我给出解决方案吗Angular 我有一个下拉列表,其中有值,我只需要选择一次值,下次必须禁用该值时,,angular,Angular,我的下拉列表有两个值或两个角色,如果我选择了一个角色,就不能再次选择相同的值。我们如何实现这一点。有人能帮我给出解决方案吗 public roleList: string[] = ["QA", "Technician"]; public separatorKeysCodes: number[] = [ENTER, COMMA]; public selectedRoles: string[] = []; public currentSelected: String; public sel
public roleList: string[] = ["QA", "Technician"];
public separatorKeysCodes: number[] = [ENTER, COMMA];
public selectedRoles: string[] = [];
public currentSelected: String;
public selectable: boolean = true;
public removable: boolean = true;
public addOnBlur: boolean = false;[![enter image description here][1]][1]
/** add CHIP's **/
add(event: MatChipInputEvent): void {
if (!this.matAutocomplete.isOpen) {
const input = event.input;
const value = event.value;
if (input) {
input.value = '';
}
}
}
/** remove CHIP's **/
remove(role): void {
const index = this.selectedRoles.indexOf(role);
if (index > 0) {
this.selectedRoles.splice(index, 1);
}
if (index <=0) {
this.selectedRoles.splice(index, 1);
this.userDiagForm.get('roles').patchValue(null)
}
}
selected(event: MatAutocompleteSelectedEvent): void {
this.selectedRoles.push(event.option.viewValue);
this.roleInput.nativeElement.value = '';
}
公共角色列表:字符串[]=[“QA”,“技师”];
公共分隔代码:数字[]=[输入,逗号];
public-selectedRoles:字符串[]=[];
所选公共数据:字符串;
公共可选:布尔值=真;
public:boolean=true;
public addOnBlur:boolean=false;[![在此处输入图像描述][1][1]
/**加芯片的**/
添加(事件:MatChipInputEvent):无效{
如果(!this.matAutocomplete.isOpen){
常量输入=event.input;
常量值=event.value;
如果(输入){
input.value='';
}
}
}
/**移除芯片的**/
删除(角色):无效{
const index=this.selectedRoles.indexOf(角色);
如果(索引>0){
这个.selectedRoles.splice(索引,1);
}
如果(index我找到了这个问题的解决方案,谢谢
我添加了一个if条件,当前所选值存在于所选角色数组中,使inputvalue为空
selected(event: MatAutocompleteSelectedEvent): void {
this.currentSelected = event.option.viewValue;
if(!this.selectedRoles.includes(this.currentSelected)){
this.selectedRoles.push(event.option.viewValue);
}
this.roleInput.nativeElement.value = '';
}
````
selected(event: MatAutocompleteSelectedEvent): void {
this.currentSelected = event.option.viewValue;
if(!this.selectedRoles.includes(this.currentSelected)){
this.selectedRoles.push(event.option.viewValue);
}
this.roleInput.nativeElement.value = '';
}
````