Return after changing stateParams from $ ionicHistory.backView ()

I am developing an Android application using the Ionic Framework.

In this case, I have three views, view1, view2 and view3 .

Navigation will look like view1 -> view2 -> view3

When returning to view2 from view3 I need to change the stateParam param1 view2 value to perform some operations. Below is the code showing that I am changing the stateParam view2 value when I press the return button in view3

Below is the code to override the action of the subsystem of the Android device

 $ionicPlatform.registerBackButtonAction(function() { if ($state.current.name == "view3") { $ionicHistory.backView().stateParams = {param1:true}; $ionicHistory.goBack(); // moves to view2 } else { $ionicHistory.goBack(); } }, 100); 

After switching to view2 , if I press the "Back" button now, it will move to view3 , and not to view1 . Now I can only view view3 and view2 by clicking the back button without going to view1 .

How to make a back button to go to view1 from view2 Even after changing the stateParam view2 value?

+6
source share
2 answers

I finally found a solution to this problem, unfortunately it's a hack, so it's a little ugly.

The board should not only update stateParams, but also stateId.

See below answer again:

 var params = {param1:true}; var backView = $ionicHistory.backView(); var stateId = backView.stateName; for (var key in params) { if (params[key] && params[key] !== "") { stateId += "_" + key + "=" + params[key]; } } backView.stateParams = params; backView.stateId = stateId; $ionicHistory.goBack(); 

In your particular case, it could be simple:

 if ($state.current.name == "view3") { var backView = $ionicHistory.backView(); backView.stateParams = {param1:true}; backView.stateId = backView.stateName + "_param1=true"; $ionicHistory.goBack(); // moves to view2 } else { $ionicHistory.goBack(); } 
+2
source

I met the same problem, but my view2 is the root, so I disconnected back when view3 returned to view2, thus made the reverse view3 disappear

  $ionicHistory.backView().stateParams = resultObject; $ionicHistory.nextViewOptions({ disableBack: true });//this can $ionicHistory.goBack(); 

my code works well with this, and for your purpose you can add backview to view2.onenter () manually

+1
source

All Articles