typescript 选择后如何禁用垫选择选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54093933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:45:54  来源:igfitidea点击:

How to Disable Mat-Select Option once it has been selected

htmlangulartypescript

提问by Saad Ahmed

I have a mat-select and I want to disable the options once I have selected them. For eg: If I select Time then Time should be disabled in the Next mat-select. I have created a stackblitz https://stackblitz.com/edit/angular-anus7w?file=app%2Fselect-overview-example.tsof my requirement.Currently only the Key gets disabled. However in my case now the Drop down data will be dynamically generated and I cannot hard code the Values which need to be disabled.

我有一个 mat-select,一旦我选择了它们,我想禁用它们。例如:如果我选择时间,那么应该在下一个垫子选择中禁用时间。我创建了一个 stackblitz https://stackblitz.com/edit/angular-anus7w?file=app%2Fselect-overview-example.ts我的要求。目前只有 Key 被禁用。但是,在我的情况下,现在下拉数据将动态生成,我无法对需要禁用的值进行硬编码。

So what I want is that if I select a purticular value in mat-select 1 then that value should be disabled in mat-select 2. Similarly the value that I select

所以我想要的是,如果我在 mat-select 1 中选择一个特定的值,那么应该在 mat-select 2 中禁用该值。同样,我选择的值

回答by Roman Lytvynov

Please check my solution https://stackblitz.com/edit/angular-anus7w-9ovxqd?file=app%2Fselect-overview-example.ts

请检查我的解决方案https://stackblitz.com/edit/angular-anus7w-9ovxqd?file=app%2Fselect-overview-example.ts

I used Observable and an array property for store all chosen values

我使用 Observable 和一个数组属性来存储所有选择的值

Main ideas:

主要观点:

回答by Chenna

You have first bind [(ngModel)]="firstOption"in the form and later do the validation. No change event is required here

您首先[(ngModel)]="firstOption"在表单中绑定,然后进行验证。这里不需要更改事件

In componentfile:

component文件中:

firstOption = '';
secondOption = '';
thirdOption = '';
fourthOption = '';

In template html file

在模板 html 文件中

<h4>Basic mat-select</h4>
<mat-form-field>
  <mat-select placeholder="Type" [(ngModel)]="firstOption">
    <mat-option
      *ngFor="let type of typeColumn; let i = index"
      [value]="{ type: type.text, index: i }"
      [disabled]="
        type.text === secondOption.type ||
        type.text === thirdOption.type ||
        type.text === fourthOption.type
      "
    >
      {{ type.text }}
    </mat-option>
  </mat-select>
</mat-form-field>
<br />
<mat-form-field>
  <mat-select placeholder="Type" [(ngModel)]="secondOption">
    <mat-option
      *ngFor="let type of typeColumn; let i = index"
      [value]="{ type: type.text, index: i }"
      [disabled]="
        type.text === firstOption.type ||
        type.text === thirdOption.type ||
        type.text === fourthOption.type
      "
    >
      {{ type.text }}
    </mat-option>
  </mat-select>
</mat-form-field>
<br />
<mat-form-field>
  <mat-select placeholder="Type" [(ngModel)]="thirdOption">
    <mat-option
      *ngFor="let type of typeColumn; let i = index"
      [value]="{ type: type.text, index: i }"
      [disabled]="
        type.text === secondOption.type ||
        type.text === firstOption.type ||
        type.text === fourthOption.type
      "
    >
      {{ type.text }}
    </mat-option>
  </mat-select>
</mat-form-field>
<br />
<mat-form-field>
  <mat-select placeholder="Type" [(ngModel)]="fourthOption">
    <mat-option
      *ngFor="let type of typeColumn; let i = index"
      [value]="{ type: type.text, index: i }"
      [disabled]="
        type.text === secondOption.type ||
        type.text === thirdOption.type ||
        type.text === firstOption.type
      "
    >
      {{ type.text }}
    </mat-option>
  </mat-select>
</mat-form-field>
<br />

回答by Alexis Rouan

    <mat-form-field>

      <mat-select [(value)]="selected" [placeholder]="!selected  ? 'Your custom placeholder' : ''">
        <mat-option value="1">Option 1</mat-option>
        <mat-option value="2">Option 2</mat-option>
        <mat-option value="3">Option 3</mat-option>
        <mat-option value="4">Option 4</mat-option>
        <mat-option value="5">Option 5</mat-option>
      </mat-select>

    </mat-form-field>