0
我新應用程序和角度。當應用程序在背景上時取消間隔
我的應用程序使用一個間隔來刷新範圍與獲取請求到服務器。我希望該時間間隔在應用程序轉到後臺時取消。
我看到其他職位「$ onunload的=功能()」,但我不知道這是否是好或如何使用它。我嘗試:$scope.$onunload=function(){ // Make sure that the interval is destroyed when leaving app $scope.stopRefresh(); };
,但沒有奏效。
注意,與離開標籤stopRefresh()函數的工作,它取消區間...
在另一方面,有一些,我可以重新啓動的時間間隔煥回來來回的背景呢?
這裏全CONTROLER:你需要
angular.module('starter.controllers', [])
main.controller("temperatureCtrl", ["$scope", "$rootScope", "$window", "$interval", "ArduinoService", function($scope, $rootScope, $window, $interval, service) {
$rootScope.arduinoServerUrl = '';
$rootScope.arduinoServerUrl = $window.localStorage['serverUrl'];
var autoRefresh;
$scope.channels = [];
function onTabSelected() {
$rootScope.arduinoServerUrl = $window.localStorage['serverUrl'];
}
$scope.options = {
loop: false,
//effect: 'fade',
speed: 500,
}
$scope.data = {};
$scope.$watch('data.slider', function(nv, ov) {
$scope.slider = $scope.data.slider;
})
function startRefresh() {
autoRefresh = $interval(function() {
updateAjax();
}, 5000);
}
function updateAjax() {
service.getChannels(function(err, result) { //get json data
if (err) {
return alert(err);
}
// puis les mets dans le scope
$scope.channels = result.channels;
})
};
$scope.init = function() { //on load page first get data
updateAjax();
//startRefresh()
}
$scope.stopRefresh = function() {
$interval.cancel(autoRefresh);
};
$scope.restartRefresh = function() {
startRefresh();
};
$scope.$on('$ionicView.leave', function() {
// Make sure that the interval is destroyed when leaving tab
$scope.stopRefresh();
});
$scope.$onunload=function(){
// Make sure that the interval is destroyed when leaving app
$scope.stopRefresh();
};
$scope.$on('$ionicView.enter', function() {
//star or restart interval scope update
$scope.restartRefresh();
});
$scope.$on('$destroy', function() {
// Make sure that the interval is destroyed too
$scope.stopRefresh();
});
}]);
它做了一點。我是新來的角,以及從來沒有使用工廠...試圖瞭解什麼是...但你明白我想要做什麼..:P。我可以簡單地向函數注入$ interval並從那裏取消它嗎?工廠是否需要撥打電話?我知道我是一個真正的初學者...:p – Nitrof