I am new in javascript and angular. This should be a simple issue but I don't know what is wrong with my code. I don't know if i have the proper syntax for the time handler. The code just display the time only once.
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-hello-world',
templateUrl: './hello-world.component.html',
styleUrls: ['./hello-world.component.css']
})
export class HelloWorldComponent implements OnInit {
dateMessage: string;
constructor() {
setInterval(this.time_handler, 1000);
//Method 1: This works
// setInterval(()=> {
// let currentDate = new Date();
// this.dateMessage = currentDate.toLocaleTimeString();
// }, 1000);
}
ngOnInit() {
}
// Method 2: This does not work
public time_handler() {
let currentDate = new Date();
this.dateMessage = currentDate.toLocaleTimeString();
}
}