列表刷新/重新加载模板(List refresh/reload template)

编程入门 行业动态 更新时间:2024-10-28 10:35:19
列表刷新/重新加载模板(List refresh/reload template)

我是离子和面临问题的新手:我必须加载并重新加载我的模板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);

提前致谢

I'm newbie in ionic and facing problem with: I have to load and reload my template by 1 and 1 minute, and it's ok for the first time, but when the function to take the records from the server is called again, it doesn't clean the template, just add the records again, so if I have only one record on the server, it shows me two on the first minute, three on the second minute and successively.

I have tried: cache=false, window.reload, state.reload, state.go and many others tries but nothing gives the result I need, cause I simply need that the contents of the list be replaced and not added. My code:

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);

Thanks in advance

最满意答案

发生的事情之一是您没有看到更新,因为您没有消化新范围。 $ 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(); });

One of things thats happening is that you're not seeing the update because you're not digesting the new scope. $scope.$apply() and $timeout() run a digest cycle so that the scope can refresh and show new values. $http calls take time to return to the client and if you're updating the scope with delayed values you should run a digest cycle. I have cleaned up your code as well.

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

更多推荐

本文发布于:2023-07-07 09:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1061392.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   模板   列表   List   reload

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!