使用Angular2的多个字段的LocalStorage

时间:2017-12-15 01:32:20

标签: angular local-storage web-storage

我正在关注此示例:https://github.com/PillowPillow/ng2-webstorage 这对我来说很好,因为存储了boundValue并且我可以检索它。但是,我有一个机器人列表:

bot.component.html

<tr *ngFor="let bot of bots" 
        routerLink="/botlevelup/{{bot.id}}">
            <td>{{bot.id}}</td>
            <td>{{bot.lastLevel}}</td>
</tr>

botlevelup.component.html

Last Level: {{boundValue}}
<input [(ngModel)]="bot.lastLevel" /> 

botlevelup.component.ts

this.storage.store('boundValue', this.bot.lastLevel);

如何使用webstorage存储我所有机器人的值?

编辑#1:我也跟踪了英雄之旅,我能够更改仅保留会话的值。

编辑#2: 显示所有机器人的原始getBots代码段:

  getBots(): Observable<Bot[]> {
      return this.http.get<Bot[]>(this.botsUrl)
      .pipe(
      catchError(this.handleError('getBots', []))
    );
  }

1 个答案:

答案 0 :(得分:0)

您可以直接将其作为对象/数组存储在本地存储中

按照以下步骤进行,

this.storage.store('botsboundvalue', JSON.stringify(this.bots));