[英]Selected value in one dropdown should not appear in other two dropdowns in Angular material
Selected value in one dropdown should not appear in other two dropdowns in Angular material.在一个下拉列表中选择的值不应出现在 Angular 材质的其他两个下拉列表中。 How to remove the selected value in one dropdown from all other dropdowns?
如何从所有其他下拉列表中删除一个下拉列表中的选定值? (I have dynamic dropdown which gets added each time a button in clicked)
(我有动态下拉菜单,每次点击按钮时都会添加)
I tried using this https://stackblitz.com/edit/angular-dqvvf5?file=src%2Fapp%2Fapp.component.html but the logic is not working for angular material.我尝试使用此https://stackblitz.com/edit/angular-dqvvf5?file=src%2Fapp%2Fapp.component.html但该逻辑不适用于有角度的材料。
I've taken your Stackblitz and managed to get it working using mat-select
instead of the standard HTML select
.我已经使用了你的 Stackblitz 并设法使用
mat-select
而不是标准的 HTML select
让它工作。 Please see this Stackblitz for a working demo.请参阅此 Stackblitz以获取工作演示。
The main changes were in the HTML...主要的变化是在 HTML...
HTML HTML
<div *ngFor="let field of fieldArray; index as idx" style="margin-bottom: 8px">
<mat-form-field appearance="fill">
<mat-label>Select Language</mat-label>
<mat-select #selectLang (selectionChange)="selected(selectLang.value,i)">
<ng-container *ngFor="let lang of languageList" >
<mat-option *ngIf="selectLang.value === lang.name || !isSelected(lang.name)" [value]="lang.name">
{{lang.name}}
</mat-option>
</ng-container>
</mat-select>
</mat-form-field>
<button (click)="deleteFieldValue(idx, selectLang.value)" style="margin-left: 8px">X</button>
</div>
<button (click)="addFieldValue()">Add Select</button>
The Typescript is pretty much the same as in your example, except I've modified the signature of the selected()
function to read selected(value: string, idx: number)
as you're passing 2 parameters into that from your HTML. Typescript 与您的示例几乎相同,除了我修改了
selected()
函数的签名以读取selected(value: string, idx: number)
当您从 HTML 中传递 2 个参数时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.