设置超时中的角变量不刷新?

viv*_*vid 5 javascript angularjs ionic-framework

我有一个问题。功能更改后,为什么h1值不刷新?据我所知,应该对Angular进行刷新,因为它总是在更改变量时刷新,否则我走错了路?

angular.module('ionicApp', ['ionic'])
.controller('MainCtrl', function($scope) {
$scope.CurrentCount = 0;
$scope.CallCounter = function(){
    setTimeout(function(){
      console.log($scope.CurrentCount)
      if($scope.CurrentCount != 9){
      $scope.CurrentCount = $scope.CurrentCount+1;
      $scope.CallCounter();
    }},1000);
  }
});
Run Code Online (Sandbox Code Playgroud)
body {
  cursor: url('http://ionicframework.com/img/finger.png'), auto;
}

.container h1{
  text-align:center;
  color:green;
}
Run Code Online (Sandbox Code Playgroud)
<html ng-app="ionicApp">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title>Count Down</title>
    <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
    <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
  </head>
  <body ng-controller= "MainCtrl">
    <div class="container">
      <div class="spacer"></div>
      <h1>{{CurrentCount}}</h1>
      <div class="button button-positive" ng-click="CallCounter()">CountDown</div>
    </div>
    
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Ale*_*tov 3

您应该使用$timeout而不是setTimeout,因为setTimeout没有有关角度范围的信息。所以应该是

angular.module('ionicApp', ['ionic'])
    .controller('MainCtrl', function ($scope, $timeout) {
    $scope.CurrentCount = 0;
    $scope.CallCounter = function () {
        $timeout(function () {
            console.log($scope.CurrentCount)
            if ($scope.CurrentCount != 9) {
                $scope.CurrentCount = $scope.CurrentCount + 1;
                $scope.CallCounter();
            }
        }, 1000);
    }
});
Run Code Online (Sandbox Code Playgroud)