angular – 从Ionic2存储中获取多个键值

前端之家收集整理的这篇文章主要介绍了angular – 从Ionic2存储中获取多个键值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用Ionic2存储,我在自定义提供程序中完成了以下操作,我可以确认键值对已设置,我可以单独检索它们:

this.storage.set('one',1);
this.storage.set('two',2);
this.storage.set('three',3);

问题是我的一个页面中有一个函数需要访问HTTP请求的所有这三个值,所以我可以得到其中一个像这样:

this.storage.get('one').then((value) => {
    // Preform http request sending value in POST data
});

但是如何一次获得多个键值?我是否需要嵌套这些调用,或者是否有一种更简单的方法可以在一次调用中访问多个键,这样我就可以执行以下操作:

this.storage.get(['one','two','three']).then((values) => {
    console.log(values.one); // I dunno,something like this
});

解决方法

你可以简单地完成它,如下所示.

this.storage.forEach( (value,key,index) => {
    console.log("value",value);//store these values as you wish
    console.log("key",key);
    console.log("Index" index);
})

From Api Doc.

/**
   * Iterate through each key,value pair.
   * @param iteratorCallback a callback of the form (value,iterationNumber)
   * @return Promise that resolves when the iteration has finished.
   */

  forEach(iteratorCallback: (value: any,key: string,iterationNumber: Number) => any): Promise<null> {
    return this._dbPromise.then(db => db.iterate(iteratorCallback));
  }

猜你在找的Angularjs相关文章