我是 Angular JS 的初学者,我想通过 JS 实时获取日期。我知道我需要刷新,如果有人有例子的话。我很感激。
正在使用的代码:
function updateTime() {
scope.time = new Date();
}
我的 html:<span>{{time|date: 'hh:mm a'}}</span>
我现在有约会了。但是想要通过刷新实时。
最佳答案
通过使用
scope.time = new Date();
你只是将当前时间设置为时间变量
改为使用 $timeout 函数
https://docs.angularjs.org/api/ng/service/ $超时
例子
$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
关于javascript - 如何使用 Refresh 获取日期时间 Angular JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27134110/