Angularjs how can I reload the content
我有这段代码可以在页面加载时加载内容,
现在,我想知道如何通过单击按钮来重新加载内容。
您可以通过示例向我展示如何做到这一点吗?
JavaScript代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | .controller('InterNewsCtrl', function($scope, NewsService) { $scope.events = []; $scope.getData = function() { NewsService.getAll().then(function (response) { $scope.events = response; }), function (error) { } }; $scope.getData(); // load initial content. }) |
1 2 3 4 5 6 7 8 9 10 11 12 13 | Html code: <ons-toolbar fixed-style> <ons-back-button>Voltar</ons-back-button> <ons-toolbar-button><ons-icon icon="ion-android-refresh"></ons-icon></ons-toolbar-button> Internacional </ons-toolbar> |
我想您是在问如何仅从后端检索新事件。如果正确,则无需重新加载整个页面。
您已经有了一个名为getData的函数,该函数可以通过您的服务检索数据。假设您的服务没有缓存数据,只需从您的按钮中调用getData:
1 | <ons-toolbar-button ng-click="getData()"><ons-icon icon="ion-android-refresh"></ons-icon></ons-toolbar-button> |
P.S。如果确实在服务中将缓存设置为true,则可以使用
删除缓存的数据。
用于以angularJs重新加载相同页面而无需回发
如果您调用
尝试下面的代码
1 2 3 4 5 6 | $scope.getdata=function() { var currentPageTemplate = $route.current.templateUrl; $templateCache.remove(currentPageTemplate); $route.reload(); } |
并按以下方式调用它
1 | <input type="button" ng-click="getdata();" value ="refresh"/> |
希望这会有所帮助
请参考