简体   繁体   English

怎么做<mat-autocomplete>点击输入时显示

[英]How to make <mat-autocomplete> show when click on input

I use a material autocomplete form linked to a input with Angular 8我使用链接到 Angular 8 输入的材料自动完成表单

I'm new to Angular and I'm still learning.我是 Angular 的新手,我还在学习。 I've already tried some solution I found on stackoverflow without success ( eg: Material autocomplete not displaying list on click )我已经尝试了一些我在 stackoverflow 上找到的解决方案但没有成功(例如: 材料自动完成不显示点击列表

Here's my HTML page:这是我的 HTML 页面:

    <mat-form-field>
      <input
        matInput
        type="text"
        placeholder="Equipment"
        aria-label="Number"
        [formControl]="machineControl"
        [formControl]="machineFilter"
        [matAutocomplete]="auto"
        #machineinput
      />
      <mat-autocomplete #auto="matAutocomplete">
        <mat-option *ngFor="let option of filteredOptions | async" [value]="option">
          {{ option }}
        </mat-option>
      </mat-autocomplete>
    </mat-form-field>

And here's my Typescript page:这是我的打字稿页面:

export class DocumentsListComponent implements OnInit {
  machineControl = new FormControl();
  options: string[] = [];
  filteredOptions: Observable<string[]>;

  @ViewChild('machineControl', { static: true }) input: ElementRef;

 constructor(public restApi: DocumentsService) {}

  ngOnInit() {


    this.restApi.getList().then(res => {
      [...]
      for (const docs of this.Document) {
        if (this.options.some(machine => machine === docs.machine)) {
        } else {
          this.options.push(docs.machine);
        }
      }
    });

    this.filteredOptions = this.machineControl.valueChanges.pipe(
      startWith(''),
      map(value => this._filter(value))
    );

    private _filter(value: string): string[] {
        const filterValue = value.toLowerCase();

        return this.options.filter(option => 
        option.toLowerCase().includes(filterValue));
    }

I expect the auto-complete form to show up on input click but I need to type at least 1 character for it to show (Removing this character still show the auto-complete form)我希望自动完成表单在输入点击时显示,但我需要输入至少 1 个字符才能显示(删除这个字符仍然显示自动完成表单)

Fixed, thanks to @AkberIqbal已修复,感谢@AkberIqbal

My issue was my autocomplete was filled with an empty list before my API call returns a result.我的问题是在我的 API 调用返回结果之前,我的自动完成填充了一个空列表。

To fix this, I assign my filteredOptions in the restApi call (At the beginning because putting it at the end didn't work):为了解决这个问题,我在 restApi 调用中分配了我的 filtersOptions (一开始因为把它放在最后不起作用):

    this.restApi.getList().then(res => {
      this.dataSource = new MatTableDataSource(res);

      this.filteredOptions = this.machineControl.valueChanges.pipe(
        startWith(''),
        map(value => this._filter(value))
      );

      [...]

      for (const docs of this.Document) {
        if (this.options.some(machine => machine === docs.machine)) {
        } else {
          this.options.push(docs.machine);
        }
      }
    });

Again, thank you @AkberIqbal, I was working on this for too long and you helped me in no time.再次感谢@AkberIqbal,我在这方面工作了太久,您很快就帮助了我。

Another aproach is to remove async function in HTML and changing the type of filteredOptions from Observable to an Array:另一种方法是删除 HTML 中的 async 函数,并将 filtersOptions 的类型从 Observable 更改为数组:

 form: FormGroup; code = new FormControl(); options: Permission[]; filteredOptions: Permission[]; constructor( private dialogRef: MatDialogRef<SelectModalComponent>, private formBuilder: FormBuilder, private api: DynamicService, ) { this.form = this.formBuilder.group({ code: [null, [Validators.required]] }); this.api.getAll('/api/permission', null).subscribe((permissions: Permission[]) => { this.options = permissions; this.filteredOptions = permissions; console.log(this.options); }); } ngOnInit() { this.code.valueChanges .pipe( startWith(''), map(value => this._filter(value)) ).subscribe(ret => { this.filteredOptions = ret; }); } private _filter(value: any): Permission[] { if (this.options && typeof (value) === 'string') { return this.options.filter(option => option.code.toString().toLowerCase().includes(value.toLowerCase())); } }
 <form [formGroup]="form" class="example-form" (ngSubmit)="submitForm()"> <div fxLayoutAlign="center center" fxLayout="column"> <mat-form-field class="example-full-width"> <input type="text" placeholder="Permissões" matInput [formControl]="code" [matAutocomplete]="auto" required> <mat-autocomplete #auto="matAutocomplete"> <mat-option *ngFor="let option of filteredOptions" [value]="option.code"> {{option.code}} </mat-option> </mat-autocomplete> </mat-form-field> <button color="primary" mat-raised-button fxAlign>Salvar</button> </div> </form>

As I had the same issue and this solution was not listed, I will post mine.由于我遇到了同样的问题并且未列出此解决方案,因此我将发布我的解决方案。 For me was enough to patch the value of the input with an empty string after the data was received from the API.对我来说,在从 API 接收到数据后,用空字符串修补输入的值就足够了。

in your case add在你的情况下添加

this.restApi.getList().then(res => {
  [...]
  this.machineControl.pachValue('') // add this line
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM