2017-03-24 4 views
0

私はLocalStorageで作業しています。角2のLocalStorageキー

this.currentData = (localStorage.getItem('currentData')!==null) ? JSON.parse(localStorage.getItem('currentData')) : [ ];

をしかし、私は、配列にデータを格納だろうと私はそれがよくないと思い、それをやって:コンストラクタで、私はそれを行っていました。

constructor(){ 
if(this.currentData !== null && this.currentData !== undefined) { 
    for (var i = this.currentData.length - 1; i >= 0; i--) { 
     this.currentData = JSON.parse(localStorage.getItem('currentData-' + i)); 
     console.log('currentData if:' + this.currentData) 
    } 
} else { 
    console.log('currentData else:' + this.currentData) 
} 
} 

しかし、私はエラーを得た、TypeError: Cannot read property 'length' of undefined TypeError: Cannot read property 'length' of undefined

だから、私はそれをやりました。私はそれを行うデータを保存するには

:私はのlocalStorageから倍数件のデータを得ることができる方法

saveLocalStorage(todo){ 
    this.objects.push(todo); 
    for (var i = this.objects.length - 1; i >= 0; i--) { 
     localStorage.setItem('currentData-' + i, JSON.stringify(this.objects[i])); 
    } 
} 

? 誰でもこの問題を解決する方法を知っていますか?あなたは、コンストラクタ

に注入のlocalStorageを使用する必要があります

+0

ような変数を追加/削除することができますか? – Jesper

+0

そして、わかりやすいようにforループを実装してください。for(var i = 0; i> this.objects.length; i--) – Jesper

答えて

0

は、おそらくあなたが代わりに

this.currentData = (this.localStorage.getItem('currentData')!==null) ? JSON.parse(localStorage.getItem('currentData')) : [ ]; 

以下のようthisキーワードを忘れてしまったかもしれない、あなたは

user:any[]={name:'abc',id:1}; 
    valuFromLocalStorage:any{}; 
    constructor(private localStorageService: LocalStorageService) { 
    this.name = 'Angular2'; 
    this.localStorageService.add('a',this.user); 
    console.log(this.localStorageService.get('a')); 
    this.valuFromLocalStorage= this.localStorageService.get('a') 
    } 

LIVE DEMO、として使用することができます

+0

あなたの答えをありがとう、 'localstorage'を使用していますコンストラクタ。私はコードを更新します –

+0

あなたの答えでそれを行うと、私はデータを嘲笑してしまいます。私はそれをすることはできません。 localstorageに新しいデータを挿入すると、この新しいデータに固有のローカルストレージキーが必要です –

0

私はlocalStorageを扱うサービスを作った。多分これはあなたを助けることができる:

import { Injectable } from '@angular/core'; 

@Injectable() 
export class NpStorageService { 

    constructor(){ 
    this.checkIflocalStorageIsAvailable(); 
    } 

    private checkIflocalStorageIsAvailable =() => { 
    let type = ''; 
    try { 
     let testKey = '__np_storage_test__' + Date.now(); 
     type = 'localStorage'; 
     localStorage.setItem(testKey, 'work'); 
     localStorage.removeItem(testKey); 
     type = 'sessionStorage'; 
     sessionStorage.setItem(testKey, 'work'); 
     sessionStorage.removeItem(testKey); 
     return true; 
    } 
    catch(e) { 
     console.error('NpStorageService => Cannot find ' + type + ' on this browser.'); 
     return false; 
    } 
    } 

    get = (key?: string) => { 
    return JSON.parse(localStorage.getItem(key)); 
    } 

    set = (key: string, value: any): boolean => { 
    try { 
     localStorage.setItem(key, JSON.stringify(value)); 
     return true; 
    } 
    catch(e) { 
     return false; 
    } 
    } 

    remove = (key: string) => { 
    return localStorage.removeItem(key); 
    } 

} 
+0

ありがとう、私はそれを試してみます –

0

$のlocalStorageサービスに使用

https://github.com/gsklee/ngStorage

をそれは簡単にあなたがのlocalStorageオブジェクトと対話することを可能とするときに、データの変更ダイジェストを呼び出します。

あなたは簡単にあなたはそれをCONSOLE.LOG場合this.currentDataが含まれない何そう

$localStorage.myObject = {array: [1,2,3,4]}; 
+0

あなたの答えをありがとう、私はAngularJSを使用していません、私は角度2を使用しています。 –

関連する問題