2016-08-05 114 views
0

我是新手在離子和麪臨的問題:我必須加載和重新加載我的模板1和1分鐘,並且這是第一次可以,但是當函數從記錄服務器再次被調用,它不會清理模板,只需重新添加記錄,所以如果服務器上只有一條記錄,則會在第一分鐘顯示兩個,第二分鐘顯示兩個,然後依次顯示。列表刷新/重新加載模板

我試過了:cache = false,window.reload,state.reload,state.go和很多其他人都試過,但沒有給出我需要的結果,因爲我只是需要將列表中的內容替換而不添加。 我的代碼:

list = function() { 
$http.post(G.URL + 'page.jsp') 
    .then(function (response) { 
     $scope.total = response.data.records.length; 
     $scope.items = response.data.records; 
     $scope.counter = 1; 
    }, function (error) { $scope.items = "Nothing"; });} 

$interval(function() { 
$scope.counter += 1; 
if ($scope.counter > 59) { 
    $scope.counter = 1; 
    list(); 
    //$ionicHistory.clearCache([$state.current.name]).then(function() { $state.reload(); }); } }, 1000); 

在此先感謝

回答

1

一個東西多數民衆贊成發生的事情是,你沒有看到更新,因爲你沒有消化的新範圍。 $ scope。$ apply()和$ timeout()運行摘要循環,以便作用域可以刷新並顯示新值。 $ http調用需要一段時間才能返回到客戶端,如果您使用延遲值更新範圍,則應該運行摘要循環。我也清理了你的代碼。

.controller('Ctrl', function($scope, G, $interval, $http, $timeout) { 
    // always initialize your scope variables 
    angular.extend($scope, { 
    total: 0, 
    items: [] 
    }); 

var list = function() { 
    $http.post(G.URL + 'page.jsp') 
    .then(function (response) { 
     angular.extend($scope, { 
     total: response.data.records.length, 
     items: response.data.records 
     }); 
     $timeout(); // this is used to reload scope with new delayed variables 
    }, function (err) { 
     console.error(JSON.stringify(err)); 
    }); 
    }; 

    // call list every 1000 milliseconds * 60 = 1 minute 
    $interval(list, 1000 * 60); 

    // call list on page load so you don't wait a minute 
    list(); 
}); 
+0

tothefux,非常感謝,解決了這個問題,我一直在尋找在谷歌星期,但沒有真正解決這個問題... –

+0

沒問題盧西亞諾,很高興我能幫忙。請將答案標記爲已接受,以便我可以得到一些布朗尼分數! – tothefux