4

In a view I have a link to choose a start location like below:

<a href="#/location/start"><input type="text" ng-model="placeStart" placeholder="place to start"></a>
<input type="text" ng-model="weight" placeholder="goods weight">

and in the location page, I do choose a place, however, When I use $ionicHistory.goBack(), I could not pass the "place" back to the previous view. I also do not want to use state.go('previous view') to pass the "place", because in that way, I will lost the other input information in the previous view.

user5191854
  • 41
  • 1
  • 3

4 Answers4

2

Here is :

$ionicHistory.backView().stateId;

don't ignore to include $ionicHistory on controller

Gourav Joshi
  • 2,419
  • 2
  • 27
  • 45
kunal shaktawat
  • 1,428
  • 12
  • 21
0

There are three options which immediately come to mind. $rootScope, localStorage and using routing not goBack().

If you need the value from one view in another, and they're completely separate controllers etc then you need a way to pass them around.

You could create and then put the value into $rootScope.globals or similar.

You could store the value to localStorage before sending the user back.

You could redirect correctly to a route which allows the values to be included in the url and still show the provious page. For example the same route with and without values set, using - or 0 for not set depending on data type:

/an/example/route/-/-/
/an/example/route/0/0/
/an/example/route/123/456

Update:

There is actually a fourth way where you can send data between controllers using $broadcast and $on. The broadcast happens in the sending controller and the $on listens in the receiving controller(s) so you can send an update to values / an object etc. $on and $broadcast in angular

Community
  • 1
  • 1
Harry B
  • 2,864
  • 1
  • 24
  • 44
  • These are all true and viable suggestions. I added an answer with my preferred additional way of using a service object. – ABCD.ca Feb 22 '16 at 19:43
0

It depends on your situation but if you don't care to preserve state after a page reload which localStorage would be good for, but rather have the state be remembered just when you're going back (and not necessarily when you later navigate forward back into the view again) then I do this: I make a separate service, just an object that sticks around in which I can inject anywhere and store some variables. In Angular 1 this would be a service object.

angular.module('foo').factory("placeHelper", [
    () => {
        let _place = null;

        class PlaceHelper {
            set place(place){
                _place = place;
            }

            get place(){
                return _place;
            }

            reset(){
                _place = null;
            }
        }

        let helper = new PlaceHelper();

        return helper;
    }
]);

Then in your controller that you're going back to, you inject placeHelper and ask it for the place and restore your state via $scope.place = placeHelper.place and when in the UI for that controller someone selects a place, you just store it in the service, placeHelper.place = $scope.place.

I would use localStorage within the service if I wanted to keep the state around after a page refresh.

I don't like polluting $rootScope because it's harder to keep track after you start to have more than a few unrelated methods in there and your properties need to have longer names (or get grouped in objects anyway). It's better for maintainability to encapsulate and separate concerns.

Service variation: The service could be an object literal instead of a class and you could directly set the properties instead of using methods if you wanted it to be a bit more simple.

angular.module('foo').factory("placeHelper", [
    () => {
        let helper = {
            place: null,

            reset(){
                this.place = null;
            }
        };

        return helper;
    }
]);
ABCD.ca
  • 2,365
  • 3
  • 32
  • 24
0

I stumbled on the same problem recently. I ended up deciding to use $ionicHistory.currentView() and $ionicHistory.backView() (see documentation here). The former function returns an object associated to the current view, while the latter returns an object associated to the view you will go to after you call $ionicHistory.goBack().

Before calling $ionicHistory.goBack() on your location page you call $ionicHistory.backView() and define a new property to the returned object whose contents is the data you want to propagate to the other view.

On your other view, you change its '$ionicView.enter' event handler so it calls $ionicHistory.currentView(), which retrieves the object with the data you want.

nfgf
  • 551
  • 6
  • 13