如何在 ngStorage,$localstorage 中传递一个简单的数组数据?

How to pass a simple array data in ngStorage,$localstorage?

所以假设我想定义一个 var array=['a','b','c']

如何将此数组传递到 localStorage 中?

我应该只写$localStorage.data = array;吗?

以及如何检查数组 $localStorage.data 是否为空且未定义?

How do I pass this array into localStorage ? Should I just simply write $localStorage.data = array;?

是的,你可以这样说:

$localStorage.data = array

And also how to check if that array $localStorage.data is not empty and not undefined?

像简单变量一样检查它

if($localStorage.data){
    console.log('empty');
} else{
    console.log('Data', $localStorage.data);
}

ngStorage

Right from AngularJS homepage: "Unlike other frameworks, there is no need to [...] wrap the model in accessors methods. Just plain old JavaScript here." Now you can enjoy the same benefit while achieving data persistence with Web Storage.

您可以简单地在本地存储中存储为:

localStorage.setItem("data", JSON.stringify(array));

稍后将其检索为

$scope.data= JSON.parse(localStorage.getItem("data"));

您可以通过

检查$scope.data
console.log($scope.data);