Reputation: 123
I am beginner in Angular JS and I would like to get the date in real time angular via JS. I know that I'll need to refresh, if anyone has an example. I am grateful.
Code in Use:
function updateTime() {
scope.time = new Date();
}
my html: <span>{{time|date: 'hh:mm a'}}</span>
I have the date now. But want real time via refresh.
Upvotes: 1
Views: 1735
Reputation: 801
by using
scope.time = new Date();
you are just setting the current time to time variable
Instead use $timeout function
https://docs.angularjs.org/api/ng/service/$timeout
Example
$scope.time = new Date();
$scope.runTimer = function () {
$scope.time = new Date();
mytimer = $timeout($scope.runTimer, 1000);
}
var mytimer = $timeout($scope.runTimer, 1000);
// on destroy call $timeout.cancel(mytimer); to stop the clock
Upvotes: 3