angularjs make a simple countdown

后端 未结 10 1546
别跟我提以往
别跟我提以往 2020-12-12 19:26

I would like make a countDown with Angular js. this is my code:

Html File

{{countDown}}
10条回答
  •  一生所求
    2020-12-12 19:55

    You probably didn't declare your module correctly, or you put the function before the module is declared (safe rule is to put angular module after the body, once all the page is loaded). Since you're using angularjs, then you should use $interval (angularjs equivalence to setInterval which is a windows service).

    Here is a working solution:

    angular.module('count', [])
      .controller('countController', function($scope, $interval) {
        $scope.countDown = 10;
        $interval(function() {
          console.log($scope.countDown--);
        }, 1000, $scope.countDown);
      });
    
    
    
    
      
    {{countDown}}

    Note: it stops at 0 in the html view, but at 1 in the console.log, can you figure out why? ;)

提交回复
热议问题