AngularJS 当前时间

AngularJS Current Time

如何在我的项目中添加当前时间? 我的控制器和模块:

var app = angular
    .module("persModul", [])
    .controller("persController", function($scope, $http)

只需使用 new Date() 并绑定 HTML

DEMO

var app = angular.module("persModul", [])
app.controller("persController", function($scope){
      $scope.timeNow = new Date();
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="persModul" ng-controller="persController">
  {{timeNow  | date:'MM/dd/yyyy @ h:mma'}}
</body>

更新的演示

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

module.controller('TimeCtrl', function($scope, $interval) {
  var tick = function() {
    $scope.clock = Date.now();
  }
  tick();
  $interval(tick, 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.min.js"></script>

<div ng-app="myApp">
  <div ng-controller='TimeCtrl'>
    <p>{{ clock | date:'HH:mm:ss'}}</p>
  </div>
</div>

您可以将间隔设置为一秒以获取更新时间,如下所示:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="datCtrl">

<p>Current Date & Time = {{ currentDateTime | date:'MM-dd-yyyy HH:mm:ss' }}</p>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('datCtrl', function($scope, $interval) {
    var getCurrentDate = function(){
     $scope.currentDateTime = new Date();
    }
    $interval(getCurrentDate, 1000);
});
</script>
</body>
</html>