效果圖:
------------------------------------------------------------------------------------------------------------------------------
1、網上不正常的
<body ng-app="App">
<div ng-controller="DemoCtrl">
<p>當前時間爲: {{now|date:'yyyy-MM-dd hh:mm:ss'}}</p>
<button ng-click="stop()">停止</button>
<button ng-click="start()">開始</button>
</div>
<script>
var App = angular.module('App', []);
App.controller('DemoCtrl', ['$scope', '$timeout', '$interval', function ($scope, $timeout, $interval) {
$scope.now = new Date();
var timer = $interval(function () {
$scope.now = new Date();
}, 1000);
$scope.stop = function () {
$interval.cancel(timer);
}
$scope.start = function () {
timer;
}
}])
</script>
2、自己研究出來的
<body ng-app="App" ng-controller="DemoCtrl" ng-init="start()">
<div>
<p>當前時間爲: {{now|date:'yyyy-MM-dd HH:mm:ss'}}</p>
<button ng-click="stop()">停止</button>
<button ng-click="start()">繼續</button>
</div>
<script>
var App = angular.module('App', []),
timer;
App.controller('DemoCtrl',function ($scope, $interval) {
$scope.now = new Date();
$scope.start=function(){
//stop();
timer = $interval(function () {
$scope.now = new Date();
}, 1000);
$scope.stop = function () {
$interval.cancel(timer);
}
}
})
</script>
總結:自己想想什麼原因吧!