2015-09-24 5 views
7

1分後に更新されたビューがあり、このビューを終了する前にタイマーを停止しても問題ありません。 現在のビューに戻ると、タイマーは再び再開しません。AngularJs&Ionic:変更するとタイムアウトを再開し、表示に戻る

これは、このビューのコントローラのコードです:あなたが戻って現在のビューに行くとき

.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"; 

}); 

答えて

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); 
    }); 
    }(); 
    }); 
+0

それが問題でした。角度サービス[$ interval](https://docs.angularjs.org/api/ng/service/$interval)をいつでも使用できます。 – LeftyX

0

、それがキャッシュから来ているので、コントローラが再び動作しません。

$ionicConfigProvider.views.maxCache(0); 

か、 キャッシュadddingによってルーティング一部に特定のビューにキャッシュを無効にすることができます:あなたは、このコード行を追加することで、あなたのアプリのconfigセクションでキャッシュを無効にすることができます偽のプロパティを。私はこの問題を解決する

詳しい情報herehere

+0

I現在のビューに偽すでに設定されているキャッシュ:私が設定されているインデックス・テンプレートに : <イオンビューキャッシュ・ビュー=「false」のタイトル=「テストアプリケーション」> ..... ... – ingalb

+0

キャッシュを無効にする.configバージョンを試しましたか? – Emre

-1

があなたのコントローラ機能は、ビューの変化に呼び出すことはありません。 var update関数の外で$ timeout関数を呼び出します。ビューがロードされるたびに、そのコントローラを呼び出して、スコープ内で匿名関数または自己実行関数を呼び出します。

.controller('IndexCtrl', function($scope, $timeout, RestService) { 
var updateN = 60 * 1000; 
$scope.test = "View 1 - Update"; 
var update = function update() { 
    var 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() { 

    timer() 
}); 

})

.controller( 'ViewCtrl2'、関数($スコープ){

$scope.test = "View 2"; 

})。

関連する問題