jamil
jamil

Reputation: 453

Angularjs how can I reload the content

I have this code that loads the content when the page load, Now I want to know how to reload the content by clicking the button. Can you show me how to do it with example please?

Javascript code:

.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.

})
Html code:

<ons-toolbar fixed-style>
      <div class="left">
        <ons-back-button>Voltar</ons-back-button>
      </div>

      <div class="right">
      <ons-toolbar-button><ons-icon icon="ion-android-refresh"></ons-icon></ons-toolbar-button>
      </div>

      <div class="center">Internacional</div>
  </ons-toolbar>

Upvotes: 1

Views: 1820

Answers (2)

jme11
jme11

Reputation: 17374

I think you're asking how to just retrieve new events from the backend. If that's correct, you don't need to reload the entire page.

You already have a function called getData which goes and retrieves you data via your service. Assuming your service doesn't cache the data, just call getData from your button:

<ons-toolbar-button ng-click="getData()"><ons-icon icon="ion-android-refresh"></ons-icon></ons-toolbar-button>

P.S. if you do explicitly have the cache set to true in your service, you can remove the cached data with $cacheFactory.get('$http').removeAll();.

Upvotes: 2

Chander .k
Chander .k

Reputation: 541

For reloading same page in angular Js without post back

first remove that url from template cache if you call $route.reload() without removing it from $templateCache it will get it from cache and it will not get latest content

Try following code

$scope.getdata=function()
{
    var currentPageTemplate = $route.current.templateUrl;
    $templateCache.remove(currentPageTemplate);
    $route.reload();
}

and Call it as following

<input type="button" ng-click="getdata();" value ="refresh"/>

Hope this will help

Please reffer this

Upvotes: 0

Related Questions