Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic $ionicHistory.goBack with different $stateParams

TL;DR

Is there a way to use $ionicHistory.goBack(-2) changing the $stateParams of the state I'm going back to?


I'm using Ionic to develop a small app, but I'm facing the following problem:

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

productInfo state

$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, theres a comparison button where the user goes to the selection, and chooses another product to be compared with the first one.

When the user confirms his selection in the selection state, the app goes to the last state, which is comparison, displaying the results for both products.

In this last state (comparison), if the user clicks one of the products, the application has to go back to state 1, productInfo, with the clicked product selected.

I'm using $ionicHistory.goBack(-2) and not $state.go('productInfo') to avoid messing with my back buttons (every screen has a default button, which simply calls $ionicHistory.goBack()).

The problem kicks in: I can't change the $stateParams of the productInfo state and so the last product selected is shown.

The ideal scenario would be something 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 that creates a new history, and thus does not work as intended.

like image 298
tpsilva Avatar asked Aug 04 '16 18:08

tpsilva


1 Answers

Have you tried clearing out the previous two views ('productInfo', 'comparison') and then going to the requested state. It'd be something like:

$ionicHistory.removeBackView();
$ionicHistory.removeBackView();
$state.go('productinfo', {product: <selected-product>});
like image 151
radyz Avatar answered Oct 29 '22 22:10

radyz