Ionic $ ionicHistory.goBack with different $ stateParams

TL DR

Is there a way to use $ ionicHistory.goBack (-2) that changes $ stateParams of the state I'm returning to?


I use Ionic to develop a small application, but I encounter the following problem:

I have several states that are part of the whole process: productInfo, selection and comparison. The user selects the product, then it is displayed in state productInfo. The selected product is passed to $stateParams .

Product status

 $stateProvider.state('productInfo', { url: '/compare/info', params: { product: null }, cache: false, views: { 'main-screen': { templateUrl: 'templates/modules/comparator/info.html', controller: 'CompareInfoCtrl', controllerAs: 'product' } } }) 

Inside productInfo there is a comparison button, in which the user goes to the selection, and selects another product to compare with the first.

When the user confirms his choice in the selection state, the application enters the last state, which is a comparison, displaying the results for both products.

In this last state (comparison), if the user clicks one of the products, the application should return to state 1, productInfo, with the selected click.

I use $ionicHistory.goBack(-2) and not $state.go('productInfo') to avoid $state.go('productInfo') with my back buttons (there is a default button on every screen that just calls $ ionicHistory.goBack ()).

The problem works: I cannot change $stateParams state of productInfo and therefore the last selected product is displayed.

An ideal script would be like $ionicHistory.goBack(-2, {product: obj}) .

My last shot was:

 this.$ionicHistory.viewHistory().views[this.$ionicHistory.backView().backViewId].stateParams.id = this.selectedProduct.id; this.$ionicHistory.goBack(-2); 

but it creates a new story and therefore does not work properly.

+6
source share
1 answer

You tried to clear the previous two views ("productInfo", "comparison"), and then switch to the requested state. It will be something like:

 $ionicHistory.removeBackView(); $ionicHistory.removeBackView(); $state.go('productinfo', {product: <selected-product>}); 
+1
source

All Articles