Timeout function in angularJS

前端 未结 1 1450
自闭症患者
自闭症患者 2020-12-20 09:39

I am trying to implement a simple timer in angularJS. But timeout function is not working though it is suggested by everyone.




        
相关标签:
1条回答
  • 2020-12-20 09:51

    You have to inject the $timeout service to the controller

    var myApp = angular.module("ss", []);
    myApp.controller('mainCtrl', function ($sce, $scope, $timeout) {
    
        $scope.timeInMs = 10;
    
        var countUp = function () {
            $scope.timeInMs += 500;
            $timeout(countUp, 500);
        }
        $timeout(countUp, 500);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="ss" ng-controller="mainCtrl">
        {{timeInMs}}
    </div>

    0 讨论(0)
提交回复
热议问题