我需要帮助,我想在“ 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调用发送到后端时,将使用此方法。
答案 0 :(得分:0)
您应使用诸如
之类的键值对将每个元素添加到存储中for(city in cities){
storage.set(city.key, city.id)
}
然后您可以通过使用
来检索特殊IDstorage.get(city.key) //Returns the city.id
答案 1 :(得分:0)
我最终将城市名称发送到后端,并在API帖子上进行了搜索以获取城市ID,并将该变量分配给必填的city_id字段