2017-12-18 17 views
1

내가이 라이브러리 다음하고 사용하여 새로 고침 후 작동하지 않습니다 로컬 저장소에 저장하고 webstorage를 사용하여 검색하십시오. 메모리 - data.service로컬 저장소는 Angular2 인 - 메모리 데이터

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

그러나, 새로 고침 후, 내 값은 인 - 메모리 data.service.ts

bot.component.html에 저장된 이전 내용으로 되돌립니다. TS :

import { InMemoryDbService } from 'angular-in-memory-web-api'; 

export class InMemoryDataService implements InMemoryDbService { 
    createDb() { 
    const bots = [ 
    {"id":1,"lastLevel":5}, 
    {"id":2,"lastLevel":5}, 
    {"id":3,"lastLevel":5} 
]; 
    return {bots}; 
    } 
} 

botlevelup.component.html

Bot: {{bot.id}} 
Last Level: <input [(ngModel)]="bot.lastLevel" /> 
<button (click)="save()">Save</button> 

botlevelup.component.ts

save() { 
this.storage.store('boundValue', JSON.stringify(bot)); 
} 

업데이트 된 값은 콘솔 로그를 사용하여 'boundValue'에 저장되며 새로 고침 후에도 계속 유지됩니다.

새로 고침 후 원래의 in-memory-data.service.ts 대신 'boundValue'에서 업데이트 된 값을 어떻게 검색합니까?

나는 달성 싶었 다른 경우 : 봇 1과 2에 대한 변경 사항이있는 경우, 그것은 'boundValue'에서 검색합니다
  1. 봇 3에 만들어진 변경 사항이 없을 경우
  2. , 그것은 in-memory-data.service에서 검색합니다.

편집 # 1 : 나는 업데이트가없는 경우 봇의 업데이트 된 값, 또한 로봇의 이전 값을 기준으로 테이블을 다시 반환하는 관리 검색

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

IIFC, 각'bot' 객체에 대한 키로'boundValue'를 사용하고 있습니까? 이 경우 최종 업데이트는'boundValue'와 연관된 이전 값을 덮어 쓴 b/c 만 볼 수 있습니다. – bazzells

+0

두 봇을 모두 boundValue에 이미 저장할 수있었습니다. 새로 고침 후 봇을 표시하는 방법은? – icedmilocode

+0

어떻게 스토리지에서 데이터를 가져 왔습니까? docs 패키지에서 retrieve 메소드를 사용 했습니까? – bazzells

답변

0

의 코드. 로컬 스토리지 봇의

스토어 원래 값 :

발췌문 :

export const bots: Bot[] = [ 
{"id":1,"lastLevel":5}, 
{"id":2,"lastLevel":5}, 
{"id":3,"lastLevel":5} 
] 

var local = JSON.stringify(bots); 
this.storage.store('localstore', local); 

는 모든 변경 사항을 저장이 boundValue 인 질문에, 값 업데이트를위한 로컬 저장소를 생성합니다.

this.storage.store('boundValue', JSON.stringify(bot)); 

이 localstore 및 boundValue의 값을 검색, boundValue에서 업데이트가 있는지 확인하고있는 경우 업데이트 localstore를 교체합니다.

var newValues = this.storage.retrieve('boundValue'); 
    var oldValues = this.storage.retrieve('localstore '); 

새 값이 없으면 이전 값을 반환하십시오.

그렇지
if(boundValue == null){   
    var array = $.parseJSON(oldValues); 
} 

, 배열의 이전 값을 업데이트 : 마지막으로

else{ 
     var valueUpdate = $.parseJSON('[' + newValues + ']'); 
     var array  = $.parseJSON(oldValues); 

     for (var i=0; i<array.length; i++){ 
     for (var m=0; m<valueUpdate.length; m++){ 
      if (valueUpdate[m].id == array[i].id){ 
      array[i] = valueUpdate[m]; 
      break; 
      } 
     } 
     }  
    } 

:

this.bots = array; 

그것은 최고의하지 않을 수 있습니다 나는이 값에 대한 변수 '배열을'사용 방법, 그러나 나가 생각할 수 있던 무언가이다. 제 질문에 대한 모든 의견을 주셔서 감사합니다.