代码之家  ›  专栏  ›  技术社区  ›  ufk

如何在焦点上显示自动完成选项

  •  0
  • ufk  · 技术社区  · 6 年前

    我使用组件 mat-autocomplete 用一个 mat-input 自动完成功能。

    这是mat autocomplete组件的html文件

    <form [formGroup]="carTypeFormGroup" (ngSubmit)="okButton()">
      <mat-form-field>
        <input matInput formControlName="carCompany"
               placeholder="foo" aria-label="foo" [matAutocomplete]="autoCarCompany">
        <mat-autocomplete  #autoCarCompany="matAutocomplete">
          <mat-option *ngFor="let carCompany of filteredCarCompanies | async" [value]="carCompany">
            <span>{{carCompany}}</span>
          </mat-option>
        </mat-autocomplete>
      </mat-form-field>
    ...
    

    这是组件类的代码:

    @Component({
      selector: 'app-car-type',
      templateUrl: './car-type.component.html',
      styleUrls: ['./car-type.component.scss']
    })
    export class CarTypeComponent implements OnInit {
    
      carTypeFormGroup: FormGroup;
    
      filteredCarCompanies: Observable<CarType[]>;
      filteredCarModels: Observable<CarType[]>;
      carCompanies = [];
      carCompaniesLowercase = [];
      carModels = [];
      carTypes = [];
    
     private _filterCarCompanies(value: string): CarType[] {
        if (this.carCompaniesLowercase.indexOf(value.toLowerCase()) >= 0) {
          this.mainGql.GetCarModels(value).subscribe((data: any) => {
            this.carModels = [];
            data.data.car_models.forEach((row) => {
              this.carModels.push(row.model_name);
            });
          });
        }
        const filterValue = value.toLowerCase();
        return this.carCompanies.filter(carCompany => carCompany.toLowerCase().indexOf(filterValue) === 0);
      }
    
    ngOnInit() {
        this.carTypeFormGroup = this.formBuilder.group({
          carCompany: ['', Validators.required],
          carModel: ['', Validators.required],
          carType: ['', Validators.required],
          carYear: [new Date().getFullYear(), Validators.required]
        });
        this.filteredCarCompanies = this.carTypeFormGroup.get('carCompany').valueChanges
          .pipe(startWith(''), map(carCompany => carCompany ? this._filterCarCompanies(carCompany) : this.carCompanies.slice()));
    }
    ...
    }
    

    当我检查 mat自动完成 示例 https://material.angular.io/components/autocomplete/examples ,当我关注输入元素时,我确实看到了所有的结果。。

    有什么区别?我错过了什么?

    1 回复  |  直到 4 年前
        1
  •  9
  •   ufk    6 年前

    页面加载时将执行筛选器。。但是我在graphql上加载了数据,所以数据在执行第一个过滤器之后到达。我更改了它,所以只有在收到数据后才会执行过滤器。

    谢谢Swoox帮我注意到了。

    ngOnInit() {
     ...
     this.carsService.GetCarCompanies().subscribe((data: any) => {
          this.carCompanies = [];
          this.carCompaniesLowercase = [];
          data.data.car_companies.forEach((row) => {
            this.carCompanies.push(row.company_name);
            this.carCompaniesLowercase.push(row.company_name.toLowerCase());
          });
          this.filteredCarCompanies = this.carTypeFormGroup.get('carCompany').valueChanges
            .pipe(startWith(''), map(carCompany => carCompany ? this._filterCarCompanies(carCompany) : this.carCompanies.slice()));
        });