如何将元素添加到列表中?

问题描述 投票:0回答:1

我能够将项目添加到我的列表中,但我的问题是我无法为添加的每个项目保留特定的timeStamp。基本上,如果我添加项目,我希望这显示:

05/03/2018 12:44:13 - item 3

05/03/2018 12:44:10 - item 2

05/03/2018 12:44:04 - item 1

但是我的代码一直为所有项目显示相同的TimeStamp。有谁能告诉我我想念的是什么吗?提前致谢!

这是我的代码:

PLUNKER

  <div *ngFor = "let alert of alertList">
        {{currentTimeStamp +' - '+ alert }}
  </div>
javascript
1个回答
1
投票

创建警报时,应将时间戳和消息一起存储在警报对象中。像这样:

  onSave(){
    this.alertList.splice( 0 , 0 , { 
      'message': this.alertEntered,
      'timestamp': (new Date(Date.now())).toLocaleString('en-GB', { hour12:false } ).replace(',','')
    });
  }

并像这样呈现:

  <div *ngFor = "let alert of alertList">
        {{ alert.timestamp +' - '+ alert.message }}
  </div>

编辑:PLUNKER

© www.soinside.com 2019 - 2024. All rights reserved.