如何隐藏/显示下拉式Angular2材质?

如何隐藏/显示下拉式Angular2材质?,angular,typescript,angular-material,Angular,Typescript,Angular Material,我的HTML {{first}} {{second}} 像这样更改代码 <mat-form-field class="button-spacing"> <mat-select placeholder="select" [(ngModel)]="dropDownOne"> <mat-option *ngFor="let first of test1" [value]="first"> {{ first }} </

我的HTML


{{first}}
{{second}}

像这样更改代码

<mat-form-field class="button-spacing">
    <mat-select placeholder="select" [(ngModel)]="dropDownOne">
        <mat-option *ngFor="let first of test1" [value]="first"> {{ first }}
        </mat-option>
    </mat-select>
</mat-form-field>
<mat-form-field class="button-spacing">
    <mat-select placeholder="select" [(ngModel)]="DropDownTwo" *ngIf="dropDownOne=='Two'">
        <mat-option *ngFor="let second of test2" [value]="second"> {{ second }}
        </mat-option>

    </mat-select>
</mat-form-field>

{{first}}
{{second}}

*ngIf
指令用于向DOM显示/隐藏元素。
*ngIf=“dropDownOne=='Two'”
在下拉值为'Two'时显示选择框。

如下更改代码

<mat-form-field class="button-spacing">
    <mat-select placeholder="select" [(ngModel)]="dropDownOne">
        <mat-option *ngFor="let first of test1" [value]="first"> {{ first }}
        </mat-option>
    </mat-select>
</mat-form-field>
<mat-form-field class="button-spacing">
    <mat-select placeholder="select" [(ngModel)]="DropDownTwo" *ngIf="dropDownOne=='Two'">
        <mat-option *ngFor="let second of test2" [value]="second"> {{ second }}
        </mat-option>

    </mat-select>
</mat-form-field>

{{first}}
{{second}}
*ngIf
指令用于显示/隐藏DOM中的元素。
*ngIf=“dropDownOne=='Two'”
在下拉值为'Two'时显示选择框。

只需在底部下拉列表中添加一个
*ngIf=“dropDownOne=='One'
。另外,将包装从
mat form field
更改为
div
,否则,如果底部选择列表未显示,Angular将投诉

尝试一下:

<mat-form-field 
  class="button-spacing">
    <mat-select 
      placeholder="select" 
      [(ngModel)]="dropDownOne">
        <mat-option 
          *ngFor="let first of test1" 
          [value]="first">
          {{ first }}
        </mat-option>
    </mat-select>
</mat-form-field>
<div class="button-spacing">
    <mat-select 
      placeholder="select" 
      [(ngModel)]="DropDownTwo" 
      (change)="on()" 
      *ngIf="dropDownOne === 'One'">
      <mat-option 
        *ngFor="let second of test2" 
        [value]="second">
        {{ second }}
      </mat-option>

    </mat-select>
</div>

{{first}}
{{second}}

这是给你的裁判的一封信

只需在底部下拉列表中添加一个
*ngIf=“dropDownOne==“One”
。另外,将包装从
mat form field
更改为
div
,否则,如果底部选择列表未显示,Angular将投诉

尝试一下:

<mat-form-field 
  class="button-spacing">
    <mat-select 
      placeholder="select" 
      [(ngModel)]="dropDownOne">
        <mat-option 
          *ngFor="let first of test1" 
          [value]="first">
          {{ first }}
        </mat-option>
    </mat-select>
</mat-form-field>
<div class="button-spacing">
    <mat-select 
      placeholder="select" 
      [(ngModel)]="DropDownTwo" 
      (change)="on()" 
      *ngIf="dropDownOne === 'One'">
      <mat-option 
        *ngFor="let second of test2" 
        [value]="second">
        {{ second }}
      </mat-option>

    </mat-select>
</div>

{{first}}
{{second}}

这是给你的裁判的一封信


根据第一个元素的选择,可以使用ngIf隐藏第二个元素

<mat-form-field class="button-spacing">
    <mat-select placeholder="select" [(ngModel)]="dropDownOne">
        <mat-option *ngFor="let first of test1" [value]="first"> {{ first }}
        </mat-option>
    </mat-select>
</mat-form-field>

<mat-form-field *ngIf="dropDownOne=='Two'" class="button-spacing">
    <mat-select placeholder="select" [(ngModel)]="DropDownTwo" (change)="on()" [hidden]="show" [disabled]="dropDownOne== 'One'||dropDownOne == undefined ">
        <mat-option *ngFor="let second of test2" [value]="second"> {{ second }}
        </mat-option>
    </mat-select>
</mat-form-field>

{{first}}
{{second}}
注意:将*ngIf放在mat form字段标记中很重要

这里有一个供您参考


其他信息:根据您使用第二个阵列的方式,您可能希望查看何时使用ngIf和ngShow/ngHige。基本上,ngIf完全从DOM中删除,所以有时您只想隐藏它。

您可以使用ngIf根据第一个元素的选择来隐藏第二个元素

<mat-form-field class="button-spacing">
    <mat-select placeholder="select" [(ngModel)]="dropDownOne">
        <mat-option *ngFor="let first of test1" [value]="first"> {{ first }}
        </mat-option>
    </mat-select>
</mat-form-field>

<mat-form-field *ngIf="dropDownOne=='Two'" class="button-spacing">
    <mat-select placeholder="select" [(ngModel)]="DropDownTwo" (change)="on()" [hidden]="show" [disabled]="dropDownOne== 'One'||dropDownOne == undefined ">
        <mat-option *ngFor="let second of test2" [value]="second"> {{ second }}
        </mat-option>
    </mat-select>
</mat-form-field>

{{first}}
{{second}}
注意:将*ngIf放在mat form字段标记中很重要

这里有一个供您参考


其他信息:根据您使用第二个阵列的方式,您可能希望查看何时使用ngIf和ngShow/ngHige。基本上,ngIf完全从DOM中删除,所以有时您只想隐藏它。

您可以使用hidden属性隐藏第二个下拉列表。只需将更改功能放在第一个下拉列表中。Hidden属性将从视图中隐藏向下视图,而不是从dom中隐藏向下视图。但是hidden有一些问题,因为它可能与显示属性的CSS冲突。我已经在Chrome版本72.0.3626.96中进行了测试

要修复此问题,只需添加您的样式:

[hidden] { display: none !important;}
我的HTML代码:

<mat-form-field class="button-spacing">
    <mat-select placeholder="select" [(ngModel)]="dropDownOne" 
    (selectionChange)="on($event.value)">
        <mat-option *ngFor="let first of test1" [value]="first"> {{ first }}
        </mat-option>
    </mat-select>
</mat-form-field>

<mat-form-field class="button-spacing" [hidden]="hideDropDown">
    <mat-select placeholder="select" [(ngModel)]="DropDownTwo">
       <mat-option *ngFor="let second of test2" [value]="second"> {{ second }}
       </mat-option>
    </mat-select>
</mat-form-field>

可以使用“隐藏”属性隐藏第二个下拉列表。只需将更改功能放在第一个下拉列表中。Hidden属性将从视图中隐藏向下视图,而不是从dom中隐藏向下视图。但是hidden有一些问题,因为它可能与显示属性的CSS冲突。我已经在Chrome版本72.0.3626.96中进行了测试

要修复此问题,只需添加您的样式:

[hidden] { display: none !important;}
我的HTML代码:

<mat-form-field class="button-spacing">
    <mat-select placeholder="select" [(ngModel)]="dropDownOne" 
    (selectionChange)="on($event.value)">
        <mat-option *ngFor="let first of test1" [value]="first"> {{ first }}
        </mat-option>
    </mat-select>
</mat-form-field>

<mat-form-field class="button-spacing" [hidden]="hideDropDown">
    <mat-select placeholder="select" [(ngModel)]="DropDownTwo">
       <mat-option *ngFor="let second of test2" [value]="second"> {{ second }}
       </mat-option>
    </mat-select>
</mat-form-field>