How to get the current date with angularJS with refresh on date?

1

How to make a refresh only in a "ng-binding" and not in the page, to get the current date via angularJS?

I have the following code:

function updateTime() {
                scope.time = new Date();
            }

But I need to do a refresh, so that it is synchronized with the clock, that is: Whenever I update the minute, in my html it also updates.

 <span>{{time|date: 'hh:mm a'}}</span>

I saw something about $interval but so far I did not succeed, if someone can explain how to use it, or another way to get the same result, I'm grateful.

    
asked by anonymous 25.11.2014 / 18:35

1 answer

1

The $interval ¹ implements window.setInterval ² , which executes a block of code repeatedly, within a specific range. I implemented the code below by reference to the example implemented using policies .

var app = angular.module('clock', []);

app.controller('ClockController', ['$scope', '$interval', 'dateFilter', function($scope, $interval, dateFilter) {
  $scope.currentTime = dateFilter(new Date(), 'hh:mm');
  
  var updateTime = $interval(function() {
    $scope.currentTime = dateFilter(new Date(), 'hh:mm');
  }, 1000);
}]);
<div ng-app="clock" ng-controller="ClockController">
  {{ currentTime }}
</div>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
25.11.2014 / 19:51