角材料自动完成-无法读取未定义的属性“过滤器”

时间:2019-02-09 21:00:17

标签: angular-material angular6

我正在尝试连接Angular Material自动完成并获取以下错误。

  

core.js:1673错误TypeError:无法读取未定义的属性“ filter”       在ProgramsComponent.push ../ src / app / programs / programs.component.ts.ProgramsComponent._filter(programs.component.ts:34)

代码如下:

program.component.ts

import { ProgramService } from './../program.service';
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { map, startWith } from 'rxjs/operators';

@Component({
  selector: 'app-programs',
  templateUrl: './programs.component.html',
  styleUrls: ['./programs.component.css']
})
export class ProgramsComponent implements OnInit {
  programNamesControl = new FormControl();
  programNames: string[];
  filteredOptions: Observable<string[]>;

  constructor(private programService: ProgramService) {}

  ngOnInit() {
    this.programService.getProgramNames().subscribe(data => {
      this.programNames = data;
    });

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

  _filter(value: string): string[] {
    console.log('Inside filter..');
    return this.programNames.filter(programName =>
      programName.toLowerCase().includes(value.toLowerCase())
    );
  }
}

program.component.html

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

0 个答案:

没有答案