当用户触摸输入字段时,应该会出现下拉值,但我的下拉菜单仅在我在输入中键入内容后才会出现。
这是我的 HTML 代码:
<mat-form-field class="example-chip-list" style="width:100%">
<input placeholder="Vacancy" formControlName="job" [matAutocomplete]="auto" matInput>
<mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
<mat-option *ngFor="let job of filteredJobs | async" [value]="job">
{{job?.refId}} - {{job?.title}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
这是我的类型脚本函数:
ngOnInit() {
this.getAllVacancyDetails();
this.filteredJobs = this.vacancyForm.controls['job'].valueChanges.pipe(
startWith(null),
map((possition: string | null) => possition ? this._filterJobs(possition) : this.jobs)
);
}
public getAllVacancyDetails() {
this.vacancyService.getAllvacancies().subscribe(
res => {
if (res.status == 200) {
this.jobs = res.body;
}
},
err => {
this.openSnackbar("An Error Occured while Loading Dropdown Data");
}
);
}
private _filterJobs(value: any): Job[] {
let jobsList: Job[] = new Array();
if (!(value instanceof Object)) {
const filterValue = value.toLowerCase();
this.jobs.forEach(job => {
if (job.title.toLowerCase().indexOf(filterValue) === 0) {
jobsList.push(job);
}
});
if(jobsList.length == 0){
this.vacancyForm.controls['job'].setErrors({'incorrect': true});
}
}
return jobsList;
}