javascript - 如何使用 Refresh 获取日期时间 Angular JS

标签 javascript jquery angularjs

我是 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/

相关文章:

Javascript 无法删除 Ruby on Rails 中的类

javascript - 如何使用 RequireJS、grunt 和 uglify 在我的应用程序代码中组合 CKEditor?

javascript - 使用来自 Neo4j 服务器的 JSON 数据在 Angular 中创建工厂

AngularJS:最佳实践

javascript - 如何将函数应用到某个容器?

javascript - 函数在第二次单击之前不打开弹出窗口

javascript - 将鼠标悬停在链接文本上时加载图像(CSS + Javascript)

javascript - 获取复选框值并显示它们

javascript - 带有 templateUrl 和隔离范围的 Angular Testing 指令

javascript - Array.prototype.concat 的行为不像预期的那样