gpt4 book ai didi

javascript - AngularJS/Javascript 将键分配给变量语法

转载 作者:行者123 更新时间:2023-12-03 09:39:54 26 4
gpt4 key购买 nike

由于我在本地存储中放置了一个唯一的 key ,所以我不太确定是否必须在 View 中引用它

//型号

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

// Controller

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

//View --> 我必须引用唯一的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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com