如何更新数组中的1个对象?

时间:2017-09-02 12:21:05

标签: angular typescript

这就是我的数据在html上的显示方式:

enter image description here

page.html

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

page.ts这里我从localstorage获取数据。

我想要的是 - 当我点击我的文字时,点击的数组对象的title元素应该更新,     //ARRAY STRUCTURE JUST FOR YOUR NEED //[{title:"saurabh" , description:"dd" , tagline:"tt", date:"dd"}];

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

  log(elem,index) { 
    console.log(elem); 
    console.log(index);

//WHEN I CLICK, I WANT THE TITLE TO BE UPDATED TO "CLICKED", REST ALL PROPERTIES TO REMAIN SAME

//ARRAY STRUCTURE JUST FOR YOUR NEED
//[{title:"saurabh" , description:"dd" , tagline:"tt", date:"dd"}];

    localStorage.setItem("education",JSON.stringify(this.fetchdata));
  }

1 个答案:

答案 0 :(得分:0)

您想要访问对象elem的标题,只需

log(elem,index) { 
    console.log(elem.title); 
}