我想在用户的系统日期更改时刷新页面内容。 用户在03-11-2012(11月3日)午夜访问页面,当系统日期更改为04-11-2014(11月4日)时,需要刷新内容或调用函数。
我们可以使用$interval(checksysDate(),5000)
哪里checksysDate()
将检查日期是否以 5000
为间隔更改。但是,我想知道我们是否可以使用 $scope.$watch
系统日期(仅日期,即 11 月 3 日或 4 日,而非时间)。
有什么优雅的解决方案吗??
谢谢
最佳答案
你可以做这样的事情,但它并不漂亮......
angular.module("test", []).controller("TimeCtrl", function($scope, $interval) {
$scope.seconds = new Date().getSeconds();
$scope.nowSeconds = $scope.seconds; //this one is just for presentation
$interval(function() {
this.seconds = new Date().getSeconds(); //you can put here day
}.bind($scope), 5000);
$scope.$watch('seconds', function(oldV, newV) {
if(oldV !== newV)
$scope.nowSeconds = newV; //you can put your reaction here - reload page
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="test" ng-controller="TimeCtrl">
{{nowSeconds}}
</div>
关于javascript - AngularJS:系统日期更改时调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26709381/