简体   繁体   English

我如何解决日期 angular 的问题

[英]how can i solve problem with date angular

I have a problem whit new Date(), when i add a color i see the time and the date, when i add a new color the date above resets, how can I capture the date when I add the color without resetting the above date?我有一个新日期()的问题,当我添加颜色时,我会看到时间和日期,当我添加新颜色时,上面的日期会重置,当我添加颜色时如何捕获日期而不重置上面的日期?

stackblitz project 堆栈闪电战项目

html html

<form [formGroup]="colorsForm">
  <div>
    <label for="colorName">Color Name</label>
      <input id="colorName" type="text" class="form-control" 
        formControlName="colorName" name="colorName"/>
  </div>
  <button  type="button"  (click)="addCorlo()">ADD</button>
</form>
<div
*ngFor="let color of colorsList trackBy: trackByFn; let i = index; "
(click)="setActive(color)"
[ngClass]="{'active' : color.colorName === active?.colorName}"
>
<div
> ID {{ i }} - color {{ color.colorName }}---{{dayHour | date:"dd/mm/yy, h:mm:ss"}}<button (click)="delete(i)">DELETE</button></div>
</div>


<h1>COLOR BOX</h1>
<div id="colorBox" [style]="'background:' + active?.colorName"><h2>{{ active?.colorName }}</h2></div>

ts ts

 dayHour:any;

addCorlo() {
    if (this.colorsForm.valid) {
      let color = { 
        colorName: this.colorsForm.value.colorName,
      };
      this.colorsList.push(color);
    }
    this.colorsForm.reset();
    this.active = this.colorsList[0];
    this.dayHour = Date();
   }

you need make the dayHour and array to您需要将 dayHour 和数组设置为

dayHour:any[]=[]
addCorlo(){
  ...
  this.dayHour.push(new Date())
}

and use并使用

{{dayHour[i] | date:"dd/mm/yy, h:mm:ss"}}

Or make your colorsList and array of object或制作您的颜色列表和 object 数组

addCorlo(){
  ...

  this.colorsList.push( //add an object with two properties
      {
        color:this.colorsForm.value.colorName,
        dayHour:new Date()
      });
}

And use并使用

{{color.dayHour | date:"dd/mm/yy, h:mm:ss"}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM