我使用 PrimeNG 的自动完成组件
<p-autoComplete formControlName="from" [suggestions]="listFrom" (completeMethod)="searchFrom($event)" (onSelect)="selectFrom($event)" [size]="65" placeholder="Откуда"></p-autoComplete>
按照计划,当在字段中输入城市名称时,from应该执行该函数,该函数searchFrom($event)填充listFrom城市列表。
但是,当您在字段中输入任何字符时,组件会冻结。虽然同时,从控制台的输出来看,它listFrom已经被填满了。也许问题出在承诺的某个地方?我使用 TS/JS 的时间不超过三个月,在那之前我是在使用 Java 的。
listFrom 对象:
listFrom: string[] = ['a','b','c'];
搜索功能:
public searchFrom(event) {
if (event.query.length > 2) {
ymaps.geocode(event.query, {results: 5}).then((res) => {
this.listFrom.length = 0;
res.geoObjects.each((city) => {
if (city.properties.get('metaDataProperty.GeocoderMetaData.kind') === 'locality') {
this.listFrom.push(city.properties.get('name'));
}
}).then(console.log(this.listFrom));
});
}
}
根据文档判断,您的代码有错误
ymaps-map.geoObjects.each同步并返回一个对象。你then(() => console.log(this.listFrom))在错误的地方,你需要把它移低一点。