2016-06-10 68 views
0

我需要一些幫助retrive數據...ngStorage:與可變密鑰

我需要從ngStorage角的插件localStorage的檢索數據...

如果我寫

$localStorage.temp = [{ name: "Jhon", lastname: "Pitt"}]; 
var myVar = $localStorage.temp; 

這是罰款....但如果我寫

var str = 'temp'; 
$localStorage.temp = [{ name: "Jhon", lastname: "Pitt"}]; 
var myVar = $localStorage.[str]; // this doesn't work 
myVar = $localStorage.str; // this doesen't retrieve nothing cause there's no key 'str' in local storage 

如果我寫這篇文章

var myVar = JSON.parse(window.localStorage.getItem(str)); 

它的工作原理,但我需要使用$ localStorage 我該如何解決問題?我需要這麼多......

嘗試在我working plunker

回答

2

你真的很接近,但你對$ localStorage的語法錯誤。

嘗試:

var str = 'temp'; 
$localStorage.temp = [{ name: "Jhon", lastname: "Pitt"}]; 
var myVar = $localStorage[str]; // remove the .