我有一堆使用来自 ng-storage 的 LocalStorage 的 JSON 数据,如下所示,
[Object { judul="Just", isi="Testing"}, Object { judul="To", isi="Get"}, Object { judul="Specific", isi="Data"}]
但我想获得一个特定的数据来获得“isi”值,该怎么做?
我有一堆使用来自 ng-storage 的 LocalStorage 的 JSON 数据,如下所示,
[Object { judul="Just", isi="Testing"}, Object { judul="To", isi="Get"}, Object { judul="Specific", isi="Data"}]
但我想获得一个特定的数据来获得“isi”值,该怎么做?
你可以通过向你的工厂添加一个 get 函数来做到这一点
例如 :
.factory ('StorageService', function ($localStorage) {
$localStorage = $localStorage.$default({
things: []
});
var _getAll = function () {
return $localStorage.things;
};
//-----
var _get = function (isi) {
for( var i = 0; i < $localStorage.things.length ; i++ ){
if( $localStorage.things[i].isi === isi ){
return $localStorage.things[i];
}
}
}
return {
getAll: _getAll,
get : _get
};
})
在您的控制器中,您可以通过传递对象的 id 来获取特定数据
var objectInlocal = StorageService.get(Thing.isi);