首页 > 解决方案 > 如何使用城市名称搜索离子存储并获取城市 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 调用发送到后端时使用。

标签: ionic3

解决方案


您应该使用键值对将每个元素添加到存储中,例如

for(city in cities){
   storage.set(city.key, city.id) 
}

然后你可以使用检索一个特定的 id

storage.get(city.key) //Returns the city.id

推荐阅读