Angular 在角度下拉菜单中显示日期数组

Angular 在角度下拉菜单中显示日期数组,angular,angular-material,angular-ngmodel,Angular,Angular Material,Angular Ngmodel,在Angular中,我从一个JsonFile中检索时间戳,然后我想在下拉菜单中显示它,但我不知道如何从ngOnInit()中获取变量以将其传递给变量,我用它来输入下拉菜单。我将占位符放在“来自ngOnInit()的时间戳”下面的代码中,这必须是一个字符串 以下是search.component.ts文件: times: Timestamp[] = [ { value: 'timestamp from ngOnInit()', viewValue: 'timestamp from ngO

在Angular中,我从一个JsonFile中检索时间戳,然后我想在下拉菜单中显示它,但我不知道如何从ngOnInit()中获取变量以将其传递给变量,我用它来输入下拉菜单。我将占位符放在“来自ngOnInit()的时间戳”下面的代码中,这必须是一个字符串

以下是
search.component.ts
文件:

 times: Timestamp[] = [
    { value: 'timestamp from ngOnInit()', viewValue: 'timestamp from ngOnInit()' },

  ];

  this.Service.getContent().then(results => {

      this.data = results;

  for (let i = 0; i < this.data.length; i++) {

    Object.entries(this.data[i].x_data).map(([key, value]) => {
      //timestamp
      this.xvalue = this.data[i].timestamp
   
      //converted timestamp to date & time
      this.date = moment(this.data[i].timestamp).format('DD-MM-YYYY HH:mm:ss').toLocaleString();


      this.strTimestamp = String(this.xvalue)
      this.strDate = String(this.date)

      return {
        timestamp: this.strTimestamp,
        date: this.strDate,


      }

    });

      }

    })

  }
时间:时间戳[]=[
{value:'timestamp from ngOnInit()',viewValue:'timestamp from ngOnInit()',
];
this.Service.getContent().then(results=>{
这个数据=结果;
for(设i=0;i{
//时间戳
this.xvalue=this.data[i]。时间戳
//将时间戳转换为日期和时间
this.date=moment(this.data[i].timestamp).format('DD-MM-YYYY HH:MM:ss').toLocaleString();
this.strTimestamp=字符串(this.xvalue)
this.strDate=字符串(this.date)
返回{
时间戳:this.strTimestamp,
日期:this.strDate,
}
});
}
})
}
html文件:

<form>   
    <mat-form-field appearance="fill">
      <mat-label>Select timestamp <mat-icon>access_time</mat-icon></mat-label>
      <mat-select [(ngModel)]="selectedValue" name="food">
        <mat-option *ngFor="let time of times" [value]="time.value">
          {{time.viewValue}}
        </mat-option>
      </mat-select>
    </mat-form-field>
    <p> Selected timestamp: {{selectedValue}} </p>
  </form>
  

选择时间戳访问时间
{{time.viewValue}}
所选时间戳:{{selectedValue}


给定模板,component.ts应该创建一个名为
this.times
它应该是一个数组,包含以下形式的对象:
{value:…,viewValue:…}


现在,您正在创建一个对象数组
{timestamp:…,date:…}
,但您没有将其分配给组件中的实例变量。

是的,您是对的,如何分配它,您能给我一个解决方案吗?
this.times=Object.entries…
谢谢您的好方法。如何在此Object.entries函数()中传递对象值和viewValue