0

I have listed array objects on html page, when i click on any of them i get its info, let me explain how i am doing this

my test.ts file

this.fetchdata = JSON.parse(localStorage.getItem('education'));
 log(elem) { 
    console.log(elem); 

  }

my test.html

 <ul id="elements">
          <li *ngFor="let elem of fetchdata" (click)="log(elem)">
              {{elem.title}} {{elem.description}}
          </li>
      </ul>

when i click i see this in console - enter image description here

How can i delete the clicked record from locally stored array in key education ?

2 Answers 2

3

Keep track of the index

*ngFor="let elem of fetchdata; let index = index"

deleteItem(index){
    this.fetchdata.splice(index, 1);
}
Sign up to request clarification or add additional context in comments.

4 Comments

i am trying to understand , in html i am doing this, is it ok - <li *ngFor="let elem of fetchdata; let index = index" (click)="log(elem,index)"> and in my ts file log(elem,index) { console.log(elem); console.log(index); } once i get the index number, i can use it to delete
this is working fine, but data not getting deleted from array in local storage, only being vanished from page
You are splicing the data from the array not from the actual source. If you are looking for that you need to provide more details.
after you deleted the item, you need to persist the data to localStorage again using the localStorage.setItem('education', JSON.stringify(this.fetchdata)); or something
3

Your log function should be,

log(elem :any){
   let objDelete = this.fetchdata.indexOf(elem , 0);
   if (objDelete > -1) {
    this.fetchdata.splice(objDelete, 1);
  }
}

4 Comments

This is almost correct you now have to localStorage.removeItem('education') And localStorage.setItem('education', JSON.stringfy(this.fetchdata))
Property 'splice' does not exist on type 'string'
you need to have fetchdata globally and parse it to list
also you need to update the localstorage once removed

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.