Javascript将键分配给变量语法

Javascript将键分配给变量语法

由于我在本地存储中放置了唯一密钥,因此我不确定是否必须在视图中引用它

//模型

$localStorage['uniqueKey'] =[{id:1, name:"foo"}, {id:2, name:"bar"}]


//控制器

$scope.users = $localStorage['uniqueKey'];


//查看->我是否必须引用唯一的Key?还是没关系?????????

<li ng-repeat="user in users">
{{user.name}}
</li>

最佳答案

$scope.users = $localStorage.['uniqueKey'];
                            ^^ syntax error: dot should not be here

//This should be:

$scope.users = $localStorage['uniqueKey'];
//or
$scope.users = $localStorage.uniqueKey;
//or
$scope.users = $localStorage.get("uniqueKey");


正确用法。将对象存储在localStorage中:



$localStorage.uniqueKey = angular.toJson([{id:1, name:"foo"}, {id:2, name:"bar"}]);

$scope.users = angular.fromJson($localStorage.uniqueKey);


您可以使用angular.toJson()将对象转换为字符串,并使用angular.fromJson对其进行反转。 Demo

关于javascript - AngularJS/Javascript将键分配给变量语法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31215468/

10-09 23:57