2016-04-26 8 views
1

表示する時間、分、秒(ライブ)、コントローラー内で$intervalを使用して関数を書きましたが、$scopeは私の表示を更新しません。AngularJsコントローラーのライブクロック機能

angular.module('myApp').controller('clockCtrl',function($scope,$interval){ 


$interval(callAtInterval, 1000); 
}); 


    function callAtInterval(){ 
      console.log("this text is getting printed after each second"); 
      var today = new Date(); 
      h=today.getHours(); 
      var m = today.getMinutes(); 
      var s = today.getSeconds(); 
      $scope.hour=h; 
      $scope.min=m; 
      $scope.sec=s; 


    } 

関数がコールを取得しているが、ビューは更新取得されていません。ここで

<div ng-controller="clockCtrl"> 
{{hour}}:{{min}}:{{sec}} 
</div> 

私のコントローラである:ここ

私の見解です。

答えて

2

callAtInterval機能はコントローラの外で宣言されているため、$scopeにアクセスすることはできません。

angular.module('myApp').controller('clockCtrl',function($scope,$interval){ 

    function callAtInterval(){ 
     console.log("this text is getting printed after each second"); 
     var today = new Date(); 
     h=today.getHours(); 
     var m = today.getMinutes(); 
     var s = today.getSeconds(); 
     $scope.hour=h; 
     $scope.minutes=m; 
     $scope.sec=s; 
    } 

    $interval(callAtInterval, 1000); 
}); 
+0

私はこれを正常に実行しました.Thanks Mike .. Happy Coding –

関連する問題