demo.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>AngularJS</title> <script src="angular.min.js"></script> <!-- 引入AngularJS框架 --> </head> <body ng-app="App"> <div ng-controller="DemoController"> {{information}} <br> {{now}} </div> <script> var App = angular.module('App',[]); // $timeout和$interval内置服务 App.controller("DemoController",['$scope','$timeout','$interval',function($scope,$timeout,$interval) { $scope.information = 111; $timeout(function(){ // timeout延迟执行 $scope.information = 222; },2000); var i = 0; var timer = $interval(function() { // interval定时器 $scope.now = new Date(); i++; if(i==5){ $interval.cancel(timer); // 清除定时器。 } },1000); }]); </script> </body> </html>