Angular Material中mat-autocomplete在对话框中打开面板

8

我有一个带有自动完成字段的模态框,使用Angular 5和angular material 2。当模态框加载时,第一个字段的面板总是打开的...

enter image description here

如何避免这种情况?

我尝试在另一个字段上使用自动对焦属性,但它不起作用...

<form name="indexation">
                  <br>
                  <div class="row">
                    <div class="col-md-12">
                      <mat-form-field class="index-full-width">
                        <input
                          matInput
                          type="text"
                          [(ngModel)]="patientChoice"
                          placeholder="Patient"
                          aria-label="Patient"
                          [matAutocomplete]="autoPatient"
                          [formControl]="myControl">
                        <mat-autocomplete (optionSelected)="selectPat()" #autoPatient="matAutocomplete" [displayWith]="displayFnPat">

                          <mat-option *ngFor="let patient of filteredPatients | async" [value]="patient">
                            <span>{{ patient.lastName }}</span>
                            <small>{{patient.firstName}}</small> |
                            <span>né(e) le {{ patient.dateNaissance }}</span> |
                            <small>IPP: {{patient.ipp}}</small>
                          </mat-option>
                        </mat-autocomplete>
                      </mat-form-field>
                    </div>
                  </div>
                  <br>

这个问题是由材料引起的吗?
1个回答

21

当需要显示选项且输入框获得焦点时,结果面板将自动打开。默认情况下,对话框将聚焦于第一个可聚焦元素,因此我猜测这就是为什么您的自动完成在对话框加载时打开的原因。要避免这种情况,请在启动对话框时使用MatDialogConfig选项autoFocus: false


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接