Search code examples
typescriptionic-frameworkionic3ionic-storage

Create dynamic ion cards using Ionic 3 storage data


I store some strings (comming from an input) using Ionic Storage via a service call local-storage.service.ts then I transform it to an array call users because the storage.get method accept only string. Now I would display theses items into my view using ngFor, it works for one item but I would like to display the 5 last to create an ion-card with it.

The whole ion card should be created with datas comming from users interactions ( one input and a picture comming from a camera)

Maybe using the index as let i of index

I tried that but it dosn't work:

  <h2 *ngFor="let user of users[0]">{{user}}</h2>
  <h2 *ngFor="let user of users[1]">{{user}}</h2>
  <h2 *ngFor="let user of users[2]">{{user}}</h2>
  <h2 *ngFor="let user of users[3]">{{user}}</h2>
  <h2 *ngFor="let user of users[4]">{{user}}</h2>

PS: If it is not clear enought feel free to ask me I stay around for a while

myfile.html:

  <ion-input [(ngModel)]="inputext" class="daInput" type="text"></ion-input>


  <button class="charlotte-button addUser" ion-button icon-left 
 (click)="saveData()">
      <ion-icon class="picto picto-reply"></ion-icon>
      Add a user
    </button>

        <button class="charlotte-button addUser" ion-button icon-left 
 (click)="retrieveData()">
      <ion-icon class="picto picto-reply"></ion-icon>
      Load user
    </button>

     <ion-list>

          <button ion-item>
            <ion-avatar item-start>
              <img src="./assets/todelete/avatar-ts-deadpool.png">
            </ion-avatar>
            <h2 *ngFor="let user of users">{{user}}</h2> *<== This don't work* !!
          </button>
        </ion-list>
</button>
</ion-list>

myfile.ts:

 export class myFilePage {


   inputext: string;

   users = [this.inputext];

 constructor(  public localstorageservice: LocalStorageService ) {

saveData() {
   if (!null) {
     this.localstorageservice.setLocalStorage('index', this.inputext);
     this.users.push(this.inputext);
     console.log(this.users); // Here my array is filling correctly

 }
 }

 retrieveData() {
   this.localstorageservice.getLocalStorage('index', this.users).subscribe;
   console.log(this.users);
 }
}

here is my local-storage.service.ts:

 export class LocalStorageService {

  constructor(public http: HttpClient, public storage: Storage) { }

  public setLocalStorage(path, data, params?) {
    this.storage.set(this.buildKey(path, params), data).then();
  }

  buildKey(path, params): string {
    let keyString = path;
    if (params) {
      Object.keys(params).map((key) => {
        keyString = keyString + '/' + key;
     });
    }
    return keyString;
  }

  public getLocalStorage(path, params): Observable<string> {
     return Observable.fromPromise((this.storage.get(this.buildKey(path, 
 params))));
  }

}

Solution

  • I find the solution myself :) using ion-list a loop on it as following: maybe it could help someone else:

            <ion-list *ngFor="let user of users; let i = index">
             <button ion-item>
                <ion-avatar item-start>
                  <img src="./assets/todelete/avatar-ts-deadpool.png">
                </ion-avatar>
                <h2>{{user}}</h2>
              </button>
            </ion-list>