Why screen reloads each time i visit it?


#1

E.g. i use default ionic app “tabs”.
So for the “home” tab i have this code in the controller:

.controller('DashCtrl', function($scope) {
    $scope.news = "Loading...";
	$http.get('http://data.com/news').success(function(data){
		$scope.news = data;
	})
})

html code looks like this:

<ion-view title="Dashboard">
  <ion-content class="padding">
    <h1>Dash</h1>
    <p>{{news}}</p>
  </ion-content>
</ion-view>

So when I’m visiting “home” tab after little amount of time I see result of $http request.
Then I’m going to the “Account” tab.
And then I’m going back to “home” tab again.
But I’m don’t see news that was loaded few moments ago !
I see text “Loading…” and again after few seconds news loads again.

How can i prevent this ?


#2

You aren’t caching the data that you return from the HTTP get request, so each time you reload the Dashboard you are re-GETing the data.

Look into Angular’s $cacheFactory


#3

Oh Thank You !
But if i have this code on the page:
HTML:

<h1>Dash</h1>
    <p ng-click="foo()">{{text}}</p>

JS:

$scope.text = 'hello';
	$scope.foo = function () {
		$scope.text+= ' world !';
	}

If i click on ‘hello’ it becomes ‘hello world’, but if I go to another tab and return to this one - I’ll see only ‘hello’ without ‘world’.
What should I do in this case ?