【问题标题】:Angular mat chips with auto complete not working具有自动完成功能的角垫芯片不起作用
【发布时间】:2020-01-03 20:41:56
【问题描述】:

我想在我的 Angular 项目中实现 matAutoComplete 我浏览了一些参考资料
https://stackblitz.com/edit/mat-chips-angularmaterial?file=package.json

但我没有任何自动完成选项。

//component.html

<mat-form-field class="example-chip-list">
  <mat-chip-list #chipList>
    <mat-chip
      *ngFor="let fruit of fruits"
      [selectable]="selectable"
      [removable]="removable"
      (removed)="remove(fruit)">
      {{fruit}}
      <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
    </mat-chip>
    <input
      placeholder="New fruit..."
      #fruitInput
      [formControl]="fruitCtrl"
      [matAutocomplete]="auto"
      [matChipInputFor]="chipList"
      [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
      [matChipInputAddOnBlur]="addOnBlur"
      (matChipInputTokenEnd)="add($event)">
  </mat-chip-list>
  <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
    <mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
      {{fruit}}
    </mat-option>
  </mat-autocomplete>
</mat-form-field>


<pre>{{fruits|json}}</pre>

//component.ts

export class ChipsOverviewExample {
visible = true;
  selectable = true;
  removable = true;
  addOnBlur = false;
  separatorKeysCodes: number[] = [ENTER, COMMA];
  fruitCtrl = new FormControl();
  filteredFruits: Observable<string[]>;
  fruits: string[] = [];
  allFruits: any= ['hi','hello','apple'];

  @ViewChild('fruitInput') fruitInput: ElementRef;

  constructor() {
    this.filteredFruits = this.fruitCtrl.valueChanges.pipe(
        startWith(null),
        map((fruit: string | null) => fruit ? this._filter(fruit) : this.allFruits.slice()));
        console.log(this.filteredFruits)
  }

  add(event: MatChipInputEvent): void {
    const input = event.input;
    const value = event.value;

    // Add our fruit
    if ((value || '').trim()) {
      this.fruits.push(value.trim());
    }

    // Reset the input value
    if (input) {
      input.value = '';
    }

    this.fruitCtrl.setValue(null);
  }

  remove(fruit: string): void {
    const index = this.fruits.indexOf(fruit);

    if (index >= 0) {
      this.fruits.splice(index, 1);
    }
  }

  selected(event: MatAutocompleteSelectedEvent): void {
    this.fruits.push(event.option.viewValue);
    this.fruitInput.nativeElement.value = '';
    this.fruitCtrl.setValue(null);
  }

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

    return this.allFruits.filter(fruit => fruit.toLowerCase().indexOf(filterValue) === 0);
  }
}

即使我已经在 app.module.ts 中导入了 MatAutocompleteModule 包

【问题讨论】:

  • 您提供了 stackblitz 链接。这是您尝试遵循的参考链接,还是您遇到问题的实现。
  • 是的,这是我关注的参考链接

标签: angular mat-autocomplete


【解决方案1】:

试试这样:

.ts

 constructor() {
    this.fruitCtrl.valueChanges.subscribe(search => {
      this.filteredFruits = of(this.allFruits.filter(item =>
        item.name.toLowerCase().includes(search)
      ));
    });
  }

.html

<mat-form-field class="example-chip-list">
  <mat-chip-list #chipList>
    <mat-chip
      *ngFor="let fruit of fruits"
      [selectable]="selectable"
      [removable]="removable"
      (removed)="remove(fruit)">
      {{fruit}}
      <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
    </mat-chip>
    <input
      placeholder="New fruit..."
      #fruitInput
      [formControl]="fruitCtrl"
      [matAutocomplete]="auto"
      [matChipInputFor]="chipList"
      [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
      [matChipInputAddOnBlur]="addOnBlur"
      (matChipInputTokenEnd)="add($event)">
  </mat-chip-list>
  <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
    <mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
      {{fruit.name}}
    </mat-option>
  </mat-autocomplete>
</mat-form-field>


<pre>{{fruits|json}}</pre>

Working Demo

【讨论】:

  • 它不工作,问题是我没有得到任何选项弹出对话框。我想你回答了[object-object],对吧?
  • 还有搜索过滤器。你检查过我分享的演示吗?是不是少了什么?
  • 是的,演示按预期工作,但问题是在我的情况下,我没有得到选项弹出窗口
  • 我无法理解你的问题,你能在stackbiltz中分享它
【解决方案2】:

[已更新答案以修复第二个搜索列表]

HTML 更改

<mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
  {{fruit.name}}
</mat-option>

打字稿更改

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

  return this.allFruits.filter((fruit) => new RegExp(value, 'gi').test(fruit['name']));
}

【讨论】:

  • 我没有得到,我为什么要使用 {{fruit.name}} 。在我的情况下,它只是一个字符串列表。
  • this.fruitCtrl.valueChanges 当 ctrl 没有任何值时返回 this.allFruits.slice() 意味着重新运行相同的 allFruits allFruits: string[] = [ { id: 1, name: 'Apple ' }, { id: 2, name: 'Orange' }, { id: 3, name: 'Banana' }, { id: 4, name: 'Malta' } ] 数组。请控制台日志并检查您返回的值。谢谢。
  • 我认为根据您的查询,该解决方案可以正常工作?? r8.
猜你喜欢
  • 2017-08-29
  • 2019-09-17
  • 2018-12-24
  • 1970-01-01
  • 1970-01-01
  • 2017-02-28
  • 2020-11-14
  • 2014-03-11
  • 2014-01-16
相关资源
最近更新 更多