ionic3 - 如何使用城市名称搜索离子存储并获取城市 ID
问题描述
我需要帮助,我想在“citiesData”中搜索离子本地存储,并在用户通过地理位置返回城市名称时返回 city_id。
目前,我在初始页面加载时从服务器加载了我的城市数据并保存在本地存储中。在下一页加载时,我们使用本地存储数据
getCitiesData() {
return new Promise((resolve, reject) => {
this.storage.get('token').then((value) => {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Bearer ' + value);
this.http
.get(ApiUrl + '/getCities', { headers: headers })
.pipe(map((res) => res.json()))
.subscribe(
(data) => {
this.cities = data.data;
this.storage.set('citiesData', data.data);
resolve(data);
},
(error) => {
reject(error);
}
);
});
});
}
当用户地理位置返回城市名称时,我想获取 company_id 和 city_id。这将在通过 api 调用发送到后端时使用。
解决方案
您应该使用键值对将每个元素添加到存储中,例如
for(city in cities){
storage.set(city.key, city.id)
}
然后你可以使用检索一个特定的 id
storage.get(city.key) //Returns the city.id
推荐阅读
- python - 使用 Python Click 创建分层命令
- javascript - 加载脚本和清单违反了内容安全策略指令
- java - 运行本机查询时发生 ConverterNotFoundException
- android - 如何确定 GMail 用于传递我的应用程序接收到的文件的 MimeType?
- php - 基本 Stripe 集成无法加载支付页面
- javascript - 如何在 Mongoose 中使用 findByIdAndUpdate 填充数组
- go - 带舵的尺码表
- ckeditor - TYPO3 9.5.4 CKEditor RTE 删除样式属性
- php - Get_where() 带有用户定义的子句
- ios - 如何使clang仅在发布时编译文件?