2016-08-05 171 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