您的位置:首页 > Web前端 > AngularJS

AangularJs动态时间例子

2016-05-12 15:41 453 查看
最近宝宝学AngularJs学得一脸懵逼。然后看视频写了几个例子,写得一脸懵逼,于是再写了一遍,终于觉得理解了,然而代码用得不熟(很尴尬啊~~),于是整理了一下代码准备发上来以后记不住的时候看看。。。看代码吧。。。(第一次发博客。。激动ing~~)

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>动态时间例子</title>

<script src="angular.js"></script>

<script>

var app=angular.module("timeDemo",[]);

app.controller("timeCtrl",function($scope){

//获取当前时间

var now1=new Date();

//让时间在页面显示

$scope.Now=now1.getHours()+':'+now1.getMinutes()+':'+now1.getSeconds();

//写一个方法获取当前时间

$scope.SetTimer=function(){

//angularJs的特性,需要手动把变化映射到html元素上面

$scope.$apply(function(){

var now=new Date();

//在控制台打印,可以不要

console.log($scope.Now);

$scope.Now=now.getHours()+':'+now.getMinutes()+':'+now.getSeconds();

});

};

//每隔1秒刷新一次时间

$scope.SetTimerInterval=setInterval($scope.SetTimer,1000);

});

</script>

</head>

<body ng-app="timeDemo" ng-controller="timeCtrl">

当前时间为:{{Now}}

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: