Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从AngularJs中的本地存储访问密钥中的值?_Javascript_Angularjs_Local Storage - Fatal编程技术网

Javascript 如何从AngularJs中的本地存储访问密钥中的值?

Javascript 如何从AngularJs中的本地存储访问密钥中的值?,javascript,angularjs,local-storage,Javascript,Angularjs,Local Storage,我想用AngularJs以最简单的方式访问本地存储中一些键的值。 在资源中-->本地存储中我有: Key:myKey Value:{ "layouts":[ other_things ],"states":{ other_things },"storageHash":"fs4df4d51"} 我试过: console.log($window.localStorage.key(0).valueOf('layouts')); //or console.log($window.localSto

我想用AngularJs以最简单的方式访问本地存储中一些键的值。 在资源中-->本地存储中我有:

Key:myKey
Value:{ "layouts":[ other_things ],"states":{ other_things  },"storageHash":"fs4df4d51"}
我试过:

console.log($window.localStorage.key(0).valueOf('layouts'));
  //or
console.log($window.localStorage.getItem('myKey'));
结果

我的钥匙

你可以做:

$window.localStorage['myKey']
如果数据是字符串化的(读:JSON.stringify),那么:


LocalStorage以字符串而不是对象的形式存储值。您需要在分配对象之前序列化对象,并在获取时反序列化对象

var myObject= { "layouts":[ other_things ],"states":{ other_things  },"storageHash":"fs4df4d51"};

// Stringify JSON object before storing
localStorage.setItem('myKey', JSON.stringify(myObject));

// Retrieve the object
var myRetrievedObject = JSON.parse(localStorage.getItem('testObject'));

从本地存储获取JSON数据,因此需要将其解析为JS对象,
JSON.parse($window.localStorage.myKey).layouts

Hello Vlad,我建议使用为浏览器的localStorage编写的localStorageService模块,这非常有帮助。查看url@katmanco是的,谢谢,我已经看到了该模块。我想先测试一下其他的东西,如果成功了,我会安装这个模块。
var myObject= { "layouts":[ other_things ],"states":{ other_things  },"storageHash":"fs4df4d51"};

// Stringify JSON object before storing
localStorage.setItem('myKey', JSON.stringify(myObject));

// Retrieve the object
var myRetrievedObject = JSON.parse(localStorage.getItem('testObject'));