ionic4 - 如何在搜索栏 Ionic 4 上设置值?
问题描述
我正在尝试在搜索栏上设置值,但看不到任何示例,只是我想要将列表的值放入搜索栏(执行自动完成搜索栏),我已经完成了以下代码:
HTML
<ion-searchbar type="text" debounce="500" animated
placeholder="Filtrar por país" color="dark"
(ionChange)="search($event)" clearInput></ion-searchbar>
<ion-list>
<ion-item *ngFor="let country of countries" (click)="selectCountry($country)">
{{ country }}
</ion-item>
</ion-list>
TS
search(event) {
console.log(event)
this.initializeListCountries();
console.log(this.countries)
const val = event.target.value;
if (val && val.trim() != '') {
this.countries = this.countries.filter((country) => {
return (country.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
}
selectCountry(country) {
let val = country.target.value;
}
谢谢
解决方案
在这里,您需要在类中声明selectedCountry变量并将所选国家/地区分配给该变量,然后您需要像这样在模板中绑定该变量 [value]="selectedCountry"
解决方案
HTML
<ion-searchbar type="text" debounce="500" animated
placeholder="Filtrar por país" color="dark"
[value]="selectedCountry"
(ionChange)="search($event)" clearInput></ion-searchbar>
<ion-list>
<ion-item *ngFor="let country of countries" (click)="selectCountry(country)">
{{ country }}
</ion-item>
</ion-list>
TS
selectedCountry: string;//declare global variable
selectCountry(country) {
this.selectedCountry = country; //assign value
}
推荐阅读
- c# - 来自 C# 控制器的 Angular http post formData 不受支持的媒体类型
- django - 如何在 django 管理表单中显示属于用户公司的列表段落
- javascript - 无法使用 ejs.renderfile 正确渲染图像
- python - C++ 与 Python 没有模块命名编码
- angular - 使用 REST API 角度 8
- react-native - 为什么 axios 总是落入陷阱?
- java - 尝试制作战舰,但 String[][] 板无法正确更新
- ms-access - 访问:如何实现将联系人加入旅行?
- node.js - 试图在快速 js 路由中结束代码执行
- azure - Azure Function 响应超时,并在收到所有内容之前重置(关闭)tcp 连接