【问题标题】:To enable or disable the select component on button click in angular material要启用或禁用按钮上的选择组件,请单击角度材料
【发布时间】:2018-08-30 04:25:10
【问题描述】:

我使用选择组件 2 次来选择时间,我还添加了图标作为后缀,以使操作为 addcancel。如下图所示

当我单击取消图标时,它会更改为添加图标,如下图所示。

这里是代码

HTML

 <mat-form-field class="no-line time">
                <mat-select  [(value)]="selectmonmor">
                  <mat-option *ngFor="let mondaymorning of mondaymornings"  [value]="mondaymorning.value">
                    {{mondaymorning.viewValue}}
                  </mat-option>
                </mat-select>
              </mat-form-field>

              <mat-form-field  class="no-line time">
                  <mat-select [(value)]="selectmoneve">
                    <mat-option *ngFor="let mondayevening of mondayevenings" [value]="mondayevening.value">
                      {{mondayevening.viewValue}}
                    </mat-option>
                  </mat-select>

  </mat-form-field>
   <button  mat-button  matSuffix mat-icon-button >
          <mat-icon (click)="toggleIcon()">{{icon}}</mat-icon>
    </button>

TS

  public icon = 'highlight_off';

   public toggleIcon() {
   if (this.icon === 'highlight_off') {
     this.icon = 'add_circle_outline';
   } else {
     this.icon = 'highlight_off';
     }
   }

在这里单击图标,即&lt;mat-icon (click)="toggleIcon()"&gt;{{icon}}&lt;/mat-icon&gt; 取消图标(即highlight_off)它更改为添加图标(即add_circle_outline)现在我需要在点击取消图标时禁用 2 个下拉菜单(select 组件)然后图标将更改为 add,点击 add 选择组件要启用。在冲浪时我也得到了stackblitz 链接

但这里启用/禁用操作是通过 2 个单独的按钮使用 2 次单击功能执行的,但我需要它仅由一个图标执行。我该怎么做?

【问题讨论】:

  • 这个问题如何获得 2 票和 5 次浏览。有什么可疑的
  • 很抱歉我的同事投票了,我们正在处理同一个问题。@TamoDev

标签: angular angular-material angular6


【解决方案1】:

试试这样:

DEMO

HTML:

<mat-toolbar color="primary">
    Angular Material 2 App
</mat-toolbar>
<div class="basic-container">

    <mat-form-field>
        <mat-select placeholder="Sample" [disabled]="selectDisabled">
            <mat-option *ngFor="let opt of [1, 2, 3, 4]" [value]="opt">
                Option {{ opt }}
            </mat-option>
        </mat-select>
    </mat-form-field>

    <button (click)="selectDisabled = !selectDisabled" mat-icon-button>
    <mat-icon >{{selectDisabled?'add': 'cancel'}}</mat-icon>
  </button>
    <pre>disabled: {{ selectDisabled }}</pre>
</div>

【讨论】:

  • 对不起这个问题,我需要申请很多选择组件。如何更改函数{{selectDisabled?'add': 'cancel'名称,使其不会调用其他选择组件。
  • 是的,使用 *ngFor 并为每个选择元素维护索引;
  • 我知道了,谢谢。
猜你喜欢
  • 2020-03-23
  • 1970-01-01
  • 2020-10-29
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多