I'm trying to test out html5 localStorage feature. For some reason, whenever I try to retrieve a value from storage after refreshing the page, i dont get any value. Values are getting stored locally on my click functionality. But when i refresh the page, it doesnt show the values.
src/app.component.ts File
import { Component } from '@angular/core';
export class MyItems {
value: string;
constructor(value:string){
this.value = value;
}
}
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
title = "Working with Angular";
myItems: MyItems[] = new Array();
IsForUpdate: boolean = false;
newItem: any = {};
updatedItem;
constructor(){
this.myItems.push(
new MyItems("First Value"),
new MyItems("Second Value"),
new MyItems("Third Value"),
new MyItems("Forth Value"),
new MyItems("Fifth Value")
);
localStorage.setItem("Values", JSON.stringify(MyItems));
var getValues = localStorage.getItem("Values");
}
AddItem() {
this.myItems.push(
this.newItem
);
this.newItem = {};
localStorage.setItem('dataSource', this.newItem);
localStorage.getItem('dataSource');
// console.log(localStorage.getItem('dataSource'));
}
EditItems(i){
this.newItem.value = this.myItems[i].value;
this.updatedItem = i;
this.IsForUpdate = true;
}
UpdateItem(){
let data = this.updatedItem;
for(let i=0; i < this.myItems.length; i++){
if(i == data){
this.myItems[i].value = this.newItem.value;
}
}
this.IsForUpdate = false;
this.newItem = {};
}
DeleteItem(i) {
var confirmMe = confirm('Do you want to Delete it ?');
if(confirmMe == true){
this.myItems.splice(i, 1);
} else {
alert("You cancelled the Operation");
}
}
}