我使用组件
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
,当我关注输入元素时,我确实看到了所有的结果。。
有什么区别?我错过了什么?