AngularJs&Ionic:更改并返回查看时停止/重新启动$ timeout

ing*_*alb 7 angularjs ionic-framework ionic ionic-view

我有一个在1分钟后更新的视图,我在离开此视图之前停止计时器,一切正常.返回当前视图后,计时器不会再次重启.

这是此视图的控制器的代码:

.controller('IndexCtrl', function($scope, $timeout, RestService) {
    var updateN = 60*1000;
    $scope.test = "View 1 - Update";
    var update = function update() {
         timer = $timeout(update, updateN);
         /** make a http call to Rest API service and get data **/
        RestService.getdata(function(data) {;
             $scope.items = data.slice(0,2);
         });
    }();

   /** Stop the timer before leave the view**/
    $scope.$on('$ionicView.beforeLeave', function(){
   $timeout.cancel(timer);
      //alert("Before Leave");
   });  

   /** Restart timer **/
    $scope.$on('$ionicView.enter', function(){
    $timeout(update, updateN);
      //alert("Enter");
   }); 
})

.controller('ViewCtrl2', function($scope) {

  $scope.test = "View 2";

});
Run Code Online (Sandbox Code Playgroud)

ing*_*alb 4

我解决了这个问题,缓存没有问题,但是我重新进入页面后没有调用功能更新。我将更新函数移至 $ionicView.enter 内:更正后的代码是:

   $scope.$on('$ionicView.beforeLeave', function(){
      //updateN=12000000;
      $timeout.cancel(timer);
      //alert("Leave");
   });

  $scope.$on('$ionicView.enter', function(){
      //updateN=12000000;
    var update = function update() {
    timer = $timeout(update, updateN);
    RestService.getdata(function(data) {
        //console.log(tani);
        //$scope.items = data;
        $scope.items = data.slice(0,2);
    });
   }();
   });
Run Code Online (Sandbox Code Playgroud)