i been tryi'n to use angular nowadays. i'm just hoping if someone could help me why i cant fetch localstorage data like the below codes. thanks
[
{
"name": "firstname",
"email": "email@yahoo.com"
}
]
service.js
getItem: function (item) {
var temp = localStorage.getItem(item);
if (!temp){
return [];
}
return JSON.Parse(temp);
}
controller.js
profile.push({
name: 'firstname',
email: 'rmail@yahoo.com'
});
localStorage.setItem('profiles', JSON.stringify(profile));
console.log(service.getItem('name') + ' : this should output the name');
console.log(service.getItem('email') + ' : this should output the email');
Can you please try this
var array = service.getItem('profiles');
for(var i=0;i<array.length;i++){
console.log(array[i].name, array[i].email)
}
To forego using parsing and deal with any of this you can try a factory I recently created, it let's you store and retrieve arrays and objects. Also it let's you bind to $scope.variables: