Chr*_*ian 6 angular-moment angular
我按照英雄之旅教程,我现在正在修改项目.我的项目将有很多时钟和计时器,我的第一个任务是制作一个显示UTC时间的时钟.使用MomentModule,我可以显示页面加载的时间:
<p>{{ myDate | amDateFormat: 'ddd Do HH:mm:ss' }}</p>
Run Code Online (Sandbox Code Playgroud)
但是,它并不像我想要的那样每秒更新一次.
我的DashboardComponent看起来像这样:
export class DashboardComponent implements OnInit {
heroes: Hero[] =[];
myDate: Date;
constructor(private heroService: HeroService) {}
ngOnInit(): void {
this.heroService.getHeroes()
.then(heroes => this.heroes = heroes);
this.utcTime();
}
utcTime(): void {
setInterval(function() {
this.myDate = new Date();
console.log(this.myDate); // just testing if it is working
}, 1000);
}
}
Run Code Online (Sandbox Code Playgroud)
首先,创建新的Date()是个好主意; 每一秒?无论哪种方式,有没有办法通过像观察或类似的东西每秒更新我的视图中的时间?就像我说的那样,当我完成时,我的页面上会有很多计时器和时钟.谢谢!
mic*_*yks 15
您只需使用arrowfunction而不是使用传统function callback语法,如下所示,
setInterval(() => { //replaced function() by ()=>
this.myDate = new Date();
console.log(this.myDate); // just testing if it is working
}, 1000);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14716 次 |
| 最近记录: |