How to return to the previous view with the parameter in ionic?

In the view, I have a link to select the starting location, as shown 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 on the location page, however, I choose a place. 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 this way I will lose other input in the previous view.

+4
source share
4 answers

Here:

 $ionicHistory.backView().stateId; 

do not ignore the inclusion of $ionicHistory on the controller

+2
source

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

If you need a value from one view to another, and they are completely separate controllers, etc., then you need to go through them.

You can create and then put the value in $ rootScope.globals or the like.

You can save the value in localStorage before sending the user.

You can redirect correctly to a route that allows values โ€‹โ€‹to be included in the URL and still display this page. For example, the same route with and without set values, using - or 0 for not set depending on the data type:

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

Update:

There is actually a fourth way that you can send data between controllers using $ broadcast and $ on. The transfer takes place in the send controller, and $ in the listen in the receive controller (s) so that you can send the update to the values โ€‹โ€‹/ object, etc. $ on and $ broadcast in angular

0
source

It depends on your situation, but if you do not want to save the state after reloading the page, for which localStorage would be useful, but rather, the state is remembered only when you return (and not necessarily when you later go back and forth to the view again ), then I do this: I do a separate service, just an object around which I can embed anywhere and store some variables. In Angular 1, this will 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 are returning to, you add placeHelper and request it for the place and restore your state via $scope.place = placeHelper.place , and when someone selects a place in the user interface for this controller, you just store it in placeHelper.place = $scope.place .

I would use localStorage inside the service if I wanted to maintain state after page refresh.

I don't like the contamination of $ rootScope because itโ€™s harder to keep track of when you start to have more than a few unrelated methods, and your properties should have longer names (or grouped by objects anyway). This is best for serviceability for encapsulation and separation of problems.

Service change: A service can be an object literal instead of a class, and you can directly set properties instead of using methods if you want it to be a little easier.

 angular.module('foo').factory("placeHelper", [ () => { let helper = { place: null, reset(){ this.place = null; } }; return helper; } ]); 
0
source

I recently came across the same issue. I decided to use $ ionicHistory.currentView () and $ ionicHistory.backView () (see the documentation here ). The first function returns the object associated with the current view, while the last returns the object associated with the view, which you will access after calling $ ionicHistory.goBack ().

Before calling $ ionicHistory.goBack () on your location page, you call $ ionicHistory.backView () and define a new property for the returned object, the contents of which are the data that you want to distribute to another view.

In your other view, you change the $ ionicView.enter event handler to call $ ionicHistory.currentView (), which retrieves the object with the desired data.

0
source

All Articles