angularjs中的interval定時執行功能

魔豆發表於2016-02-01

一個例子,用來顯示當前實時時間,1秒鐘重新整理一次:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>interval</title>
<script type="text/javascript" src="js/angular/angular.min.js"></script>
</head>
<body ng-controller="ctrl">
<div>當前時間為:{{time}}</div>
<script>
var app = angular.module('myApp',[]);
app.controller("ctrl",function($scope,$interval){
    $interval(function(){
        var time = new Date();
        var t = time.getFullYear()+""+(time.getMonth()+1)+""+time.getDate() + "日 星期"+"日一二三四五六".charAt(time.getDay())+ " "+time.getHours() +":"+time.getMinutes()+":"+time.getSeconds();
        $scope.time=t;
    },1000);
});
</script>
</body>
</html>

 

相關文章