Timeout function in angularJS

Problem

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

<!doctype html>
<html lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular-sanitize.min.js"></script>

</head>
<body>

<div ng-app="ss" ng-controller="mainCtrl">
{{timeInMs}}
</div>

<script>
var myApp = angular.module("ss", ['ngSanitize']);
myApp.controller('mainCtrl', function ($sce, $scope) {

    $scope.timeInMs = 10;

        var countUp = function() {
            $scope.timeInMs+= 500;
            $timeout(countUp, 500);
        }

       $timeout(countUp, 500);


});

</script>

</body>
</html>

When I comment out the " $timeout(countUp, 500);" line, the code works without error but timer doesn't work. Do i need to include any more javascript file?

Thanks in advance.

Problem courtesy of: Shafaet

Solution

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>

Solution courtesy of: Arun P Johny

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.