如何根据 mat-select 选项启用输入字段>?

How to enable input fields depending on mat-select options>?

提问人:Ionut 提问时间:4/18/2023 最后编辑:Ionut 更新时间:4/18/2023 访问量:34

问:

在 a 中,我有一个表单,其中包含带有以下代码的 a:mat-tabmat-select

 <mat-form-field class="example-full-width">
     <mat-label>Database</mat-label>
          <mat-select [formControl]="types" multiple>
                    <mat-select-trigger>
                         {{(types?.value ||[''])[0]}}
                           <span *ngIf="(types.value?.length || 0) > 1" class="example-additional-selection">
                                        (+{{(types.value?.length || 0) - 1}} {{types.value?.length === 2 ? 'other' :
                                        'others'}})
                           </span>
                      </mat-select-trigger>
                <mat-option *ngFor="let type of typeList" [value]="type">{{type}}</mat-option>
           </mat-select>
      <mat-icon matSuffix>storage</mat-icon>
   </mat-form-field>

打字稿上的 mat-select 中可用的选项包括:

  types = new FormControl('');
  typeList: string[] = ['SQLite', 'Microsoft SQL Server', 'MySQL'];

在 mat-select 下面,我插入了一些输入字段mat-accordion

                  <mat-accordion>
                        <mat-expansion-panel (opened)="panelOpenState = true" (closed)="panelOpenState = false">
                            <mat-expansion-panel-header>
                                <mat-panel-title> SQLite Database </mat-panel-title>
                            </mat-expansion-panel-header>
                            <mat-form-field class="example-full-width" appearance="fill">
                                <mat-label>Database Name</mat-label>
                                <input [disabled]="!types.value[0]" id="numeDB_SQLite" type="text" matInput
                                    placeholder="Ex: Lite_DB" name="numeDB_SQLite" ngModel required=""
                                    oninvalid="this.setCustomValidity('Please enter DB name')"
                                    oninput="setCustomValidity('')">
                            </mat-form-field>
                            <mat-form-field class="example-full-width" appearance="fill">
                                <mat-label>Database string Connection</mat-label>
                                <input [disabled]="!types.value[0]" id="stringConnDB_SQLite" type="text" matInput
                                    placeholder="Ex: " name="stringConnDB_SQLite" ngModel required=""
                                    oninvalid="this.setCustomValidity('Please enter DB connection string')"
                                    oninput="setCustomValidity('')">
                            </mat-form-field>
                        </mat-expansion-panel>
                    </mat-accordion>
                    <br />
                    <mat-accordion>
                        <mat-expansion-panel (opened)="panelOpenState2 = true" (closed)="panelOpenState2 = false">
                            <mat-expansion-panel-header>
                                <mat-panel-title> Microsoft SQL Server Database </mat-panel-title>
                            </mat-expansion-panel-header>
                            <mat-form-field class="example-full-width" appearance="fill">
                                <mat-label>Database Name</mat-label>
                                <input [disabled]="!types.value[1]" id="numeDB_MSSQL" type="text" matInput
                                    placeholder="Ex: MSSQL_DB" name="numeDB_MSSQL" ngModel required=""
                                    oninvalid="this.setCustomValidity('Please enter DB name')"
                                    oninput="setCustomValidity('')">
                            </mat-form-field>
                            <mat-form-field class="example-full-width" appearance="fill">
                                <mat-label>Database string Connection</mat-label>
                                <input [disabled]="!types.value[1]" id="stringConnDB_MSSQL" type="text" matInput
                                    placeholder="Ex: " name="stringConnDB_MSSQL" ngModel required=""
                                    oninvalid="this.setCustomValidity('Please enter DB connection string')"
                                    oninput="setCustomValidity('')">
                            </mat-form-field>
                        </mat-expansion-panel>
                    </mat-accordion>
                    <br />
                    <mat-accordion>
                        <mat-expansion-panel (opened)="panelOpenState1 = true" (closed)="panelOpenState1 = false">
                            <mat-expansion-panel-header>
                                <mat-panel-title> MySQL Database </mat-panel-title>
                            </mat-expansion-panel-header>
                            <mat-form-field class="example-full-width" appearance="fill">
                                <mat-label>Database Name</mat-label>
                                <input [disabled]="!types.value[2]" id="numeDB_MySQL" type="text" matInput
                                    placeholder="Ex: MySQL_DB" name="numeDB_MySQL" ngModel required=""
                                    oninvalid="this.setCustomValidity('Please enter DB name')"
                                    oninput="setCustomValidity('')">
                            </mat-form-field>
                            <mat-form-field class="example-full-width" appearance="fill">
                                <mat-label>Database string Connection</mat-label>
                                <input [disabled]="!types.value[2]" id="stringConnDB_MySQL" type="text" matInput
                                    placeholder="Ex: " name="stringConnDB_MySQL" ngModel required=""
                                    oninvalid="this.setCustomValidity('Please enter DB connection string')"
                                    oninput="setCustomValidity('')">
                            </mat-form-field>
                        </mat-expansion-panel>
                    </mat-accordion>

我想做的是启用与从 mat-select 中选择的选项关联的输入。如果取消选择该选项,则应禁用输入字段。它可以工作,但并不完全可以。在初始化时,所有输入字段都将被禁用。

如果我按以下顺序选择 mat-select 的选项:SQLite、Microsoft SQL Server、MySQL;输入字段的启用顺序与选择选项的顺序相同。

如果按顺序选择 mat-select 的选项:Microsoft SQL Server、MySQL;首先启用与SQLite关联的输入,选择MySQL时,启用Microsoft SQL Server的字段。

如果我只选择第三个选项(MySQL),SQLite的输入字段将被启用,而不是MySQL的输入字段,因为它应该可以工作。 我做错了什么?

HTML 打字稿 mat-select mat-form-field

评论


答: 暂无答案