1

Below is the data in my local storage -

key - education value - [{"title":"saurabh","description":"dd","tagline":"tt","date":"dd"},{"title":"aman","description":"dd","tagline":"tt","date":"dd"},{"title":"jessica","description":"dd","tagline":"tt","date":"dd"},{"title":"rosh","description":"dd","tagline":"tt","date":"dd"}]

I want to show each

title , description line by line on my page

my test.ts

fetchdata = '';
  ionViewDidLoad() {

    this.fetchdata = localStorage.getItem('education'); 
// what do i do next to show this on `test.html`
  }

my test.html

<ion-header>

    <ion-navbar>
      <button ion-button menuToggle>
        <ion-icon name="menu"></ion-icon>
      </button>
      <ion-title>Education</ion-title>
    </ion-navbar>

  </ion-header>


<ion-content padding>

    <button ion-button item-right>
        <ion-icon name="md-add-circle" (click)="save();"></ion-icon>
      </button>

</ion-content>

2 Answers 2

2

First of all you can't save object in storage, but only string. So to save your object you have to:

localStorage.setItem('education', JSON.stringify(data));

and to read that as object

this.fetchdata = JSON.parse(localStorage.getItem('education'));

than you can use NgForOf Directive to iterate over array as:

<ul id="elements">
    <li *ngFor="let elem of fetchdata">
        {{elem.title}} {{elem.description}}
    </li>
</ul>
Sign up to request clarification or add additional context in comments.

8 Comments

perfect thanks, 1 thing, if i click on a name, how can i read its properties in the console, once i cana read it, i will then find a way to delete it
Bind click to <li> like: <li *ngFor="let elem of fetchdata" (click)="log(elem)"> where log is funcition inside component log(elem) { console.log("CLICKED: " + elem); }
in console i see CLICKED: [object Object]
that is because your element doesn't have toString() method in prototype, but when you put elem in separate log it will work ;) log(elem) { console.log("CLICKED"); console.log(elem); }
interesting it worked, my element doesnt have toString() method in prototype , what you tried to explain i am not clear on this, can you try to explain in a lehman language
|
2

Make use of *ngFor structural Directive to display a array of objects

<div *ngFor = "let title of fetchData">
  {{title.title}} -- {{title.description}} -- {{title.tagline}} {{title.date}}
</div>

Check out this link for the working example Link

3 Comments

Cannot find a differ supporting object '[{"title":"saurabh","description":"dd","tagline":"tt","date":"dd"}]' of type 'string'. NgFor only supports binding to Iterables such as Arrays
i have seen that but wndering what is wrong, even this.fetchdata = localStorage.getItem('education'); console.log(this.fetchdata); is showing correct result - [{"title":"saurabh","description":"dd","tagline":"tt","date":"dd"},{"title":"gaurav","description":"dd","tagline":"tt","date":"dd"}] then why this error is coming
it was down to you not parsing data which i overlooked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.