Edmo Lima
Edmo Lima

Reputation: 123

How get date time angular JS with Refresh

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

Answers (1)

CodingNinja
CodingNinja

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

Related Questions