# Restore value when going back a view

**URL:** <https://forum.ionicframework.com/t/restore-value-when-going-back-a-view/4022>\
**Category:** uncategorized\
**Created:** [May 9, 2014, 6:45pm UTC](https://forum.ionicframework.com/t/restore-value-when-going-back-a-view/4022 "2014-05-09T18:45:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikewazowski](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@mikewazowski](https://forum.ionicframework.com/u/mikewazowski)\
**Post date:** [May 9, 2014, 6:45pm UTC](https://forum.ionicframework.com/t/restore-value-when-going-back-a-view/4022/1 "2014-05-09T18:45:22Z")

</div>

I’m trying to figure out how to store a value and have it restored when going back to the view.

In essence, I have 2 pages: About and Details. You can go to Details from About page. On click, I store a value. From Details, when I go back, I want this value to restored. Basically, I don’t want it to be fully persistent throughout the app, just for this lifespan of going back to a view.

Is this possible to achieve and how? For instance, is there a way to listen to the back event?

\*\* I started looking at the source code for $ionicScrollDelegate, which has this rememberScrollPosition functionality, which is kind of what I want to achieve but with storing my own custom values. [https://github.com/driftyco/ionic/blob/b7859553483ad59b246ca36d9f91327544dd9868/js/angular/controller/scrollController.js](https://github.com/driftyco/ionic/blob/b7859553483ad59b246ca36d9f91327544dd9868/js/angular/controller/scrollController.js)

---

<div class="post-metadata">

**Author:** ![mikewazowski](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@mikewazowski](https://forum.ionicframework.com/u/mikewazowski)\
**Post date:** [May 9, 2014, 7:03pm UTC](https://forum.ionicframework.com/t/restore-value-when-going-back-a-view/4022/2 "2014-05-09T19:03:07Z")

</div>

Ok, so I’ve been reading on the $viewHistory (viewService) source code and think I found the solution.

To determine if you are going back to this view, you have to check:

if ($rootScope.$viewHistory.forwardView) {  
// Came back from details view for example.  
}

I’ve been testing this a bit and been getting exactly what I wanted. [https://github.com/driftyco/ionic/blob/b87bcb30c33fb577b41139c43089b2a864211d56/js/angular/service/viewService.js](https://github.com/driftyco/ionic/blob/b87bcb30c33fb577b41139c43089b2a864211d56/js/angular/service/viewService.js)

---

<div class="post-metadata">

**Author:** ![Calendee](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/calendee/32/13656_2.png) [@Calendee](https://forum.ionicframework.com/u/Calendee)\
**Post date:** [May 9, 2014, 7:43pm UTC](https://forum.ionicframework.com/t/restore-value-when-going-back-a-view/4022/3 "2014-05-09T19:43:08Z")

</div>

You should really be creating and using a service (factory) to do this.

VERY simplistic example :

```auto
.factory('MyService', function() {
	
	var storedValue;

	return {

		getStoredValue = function() {
			return storedValue
		},

		setStoredValue = function(value) {
			storedValue = value
		}
	}

})
.controller('MyCtrl', ['$scope', 'MyService', function($scope, MyService) {
	
	$scope.data = {
		specialValue : MyService.getStoredValue()
	}

}])

```
