简体   繁体   English

Angular/Ionic Storage,如何动态存储数据

[英]Angular/Ionic Storage, how to store data dynamically

So I have some code that allows a user to pick a contact and that data gets parsed into an array.所以我有一些代码允许用户选择联系人并将数据解析为数组。 however, the problem is there is no way of storing that data permanently, as the user leaves the component the data disappears.但是,问题是无法永久存储该数据,因为用户离开组件时,数据就会消失。

I have been trying to use Ionic storage to store that data, but my knowledge is lacking in how to do this properly.我一直在尝试使用 Ionic 存储来存储该数据,但我缺乏如何正确执行此操作的知识。

here is the relevant code.这是相关的代码。

export class ContactComponentComponent implements OnInit {

  constructor(private contacts: Contacts, private storage: Storage) {
   this.getContact();
  }

  mContacts = [
   {
    id: [0],
    name: '',
    number: ''
   },
   {
    id: [1],
    name : [''],
    number: ['']
   },
   {
    id: [2],
    name : [''],
    number: ['']
   },
   {
    id: [3],
    name : [''],
    number: ['']
   },
   {
    id: [4],
    name : [''],
    number: ['']
   }
  ];

  ngOnInit() {}

  pickContact(contactId) {
    this.contacts.pickContact().then((contact) => {
      this.mContacts[contactId].name = contact.name.givenName;
      this.mContacts[contactId].number = contact.phoneNumbers[0].value;
      this.storage.set('contact-name', JSON.stringify(this.mContacts.name));
      this.storage.set('contact-number',  JSON.stringify(this.mContacts.number));
    });
  }

  getContact()
  {
    this.storage.get('contact-name').then((val) => {
      console.log('Contact Name: ', val);
    });
    this.storage.get('contact-number').then((val) => {
      console.log('Contact Number:', val);
    });
  }

}

Here is the HTML这是 HTML

<ion-list>

  <ion-grid>
    <ion-row>
      <ion-col>
        <ion-item-group   *ngFor="let contacts of mContacts">
          <ion-card (click) = "pickContact(contacts.id)">
              <ion-item lines = "none">             
                  <ion-label class="ion-text-wrap">Name: {{contacts.name}}</ion-label>        
                </ion-item>
                <ion-item lines = "none" >
                  <ion-label class="ion-text-wrap">Number: {{contacts.number}}</ion-label>
                </ion-item>       
          </ion-card>            
        </ion-item-group>    

      </ion-col>
    </ion-row>

  </ion-grid>
</ion-list>

I know this is terrible code, but I have no idea how to save this array after the user has selected it and retrieve it, when the component is called我知道这是糟糕的代码,但我不知道如何在用户选择并检索它之后保存这个数组,当组件被调用时

I finally discovered an answer, Hope this helps someone.我终于找到了答案,希望这对某人有所帮助。


  constructor(private contacts: Contacts, private storage: Storage) {
   this.getContact();
  }
  key:string = "contacts";
  mContacts = [
   {
    id: [0],
    name: '',
    number: ''
   },
   {
    id: [1],
    name : [''],
    number: ['']
   },
   {
    id: [2],
    name : [''],
    number: ['']
   },
   {
    id: [3],
    name : [''],
    number: ['']
   },
   {
    id: [4],
    name : [''],
    number: ['']
   }
  ];

  ngOnInit() {}

  pickContact(contactId) {

    this.contacts.pickContact().then((contact) => {
      this.mContacts[contactId].name = contact.name.givenName;
      this.mContacts[contactId].number = contact.phoneNumbers[0].value;
      this.saveContacts();
    });
  }
  saveContacts(){
    this.storage.set(this.key, JSON.stringify(this.mContacts));
  }
  getContact()
  {
    this.storage.get(this.key).then((val) => {
      console.log('Contact Name: ', val);
      if (val != null && val !== undefined) {
        this.mContacts = JSON.parse(val);
      }
    });

  }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在angular2的本地存储中存储数据 - how to store data in local storage in angular2 如何使用Ionic Native Storage存储阵列数据? - How to Store Array Data Using Ionic Native Storage? ionic 3角度数据不会动态变化 - ionic 3 angular data is not changing dynamically 如何使用Ionic 2/3在angular 2/4中动态设置表格标题和数据? - How to set table heading and data dynamically in angular 2/4 using Ionic 2/3? 如何动态地将数据值添加到生成的 HTML - Ionic Angular - How to dynamically add data values to generated HTML - Ionic Angular 如何在Angular4 / Ionic 3中的多维数组中存储和检索数据 - How to Store and Retrieve Data in Multidimensional Array in Angular4 / Ionic 3 如何使用会话存储在Angular 6上存储和检索数据? - How to store and retrieve data on Angular 6 using Session Storage? 如何在Angular 2的本地存储中存储“文件”类型的数据 - How to store 'File' type data in local Storage in Angular 2 如何将数据数组对象保存到离子SQlite存储(TypeScript,Angular 5,Ionic 3) - How to save a data array object to ionic SQlite storage (TypeScript, Angular 5, Ionic 3) 如何使用Ionic Storage使用Ionic 3从api存储json对象? - How to use Ionic Storage to store json object from api with ionic 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM