角度材料垫自动完成打开面板

时间:2018-04-30 11:23:44

标签: angular autocomplete focus angular-material

我有自动填充字段的模态。 我使用Angular 5和角度材料2。 加载模态时,第一个字段的面板始终打开...

enter image description here

如何避免这种情况?

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

<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 个答案:

答案 0 :(得分:6)

当有要显示的选项且字段具有焦点时,结果面板将自动打开。默认情况下,对话框会将焦点设置在第一个可聚焦元素上,因此我认为这就是对话框加载时自动完成功能打开的原因。要避免这种情况,请在启动对话框时使用MatDialogConfig选项autoFocus: false