0

i've got a view that's outside of my tabs, now when i try to redirect from that view to a tab i get redirected to the wrong tab. i've made a codepen to show you what i mean since i don't really know how to explain it well see the logs i create in the console

  .controller('MyCtrl', function($scope, $state, $rootScope, $ionicPlatform) {
     $rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){ 
            // do something
            $scope.from = fromState;
            console.log(fromState);
        })

        $scope.goBack = function(){
            console.log($scope.from.name);
            $state.go($scope.from.name);
        }

    })

Codepen

Sjoerd de Wit
  • 2,353
  • 5
  • 26
  • 45

4 Answers4

1

You should apply the other answers but you have another problem.

The other problem is that, when you click on back button the state is changing to tab.volgend (which is right) but after this is going to tab.alert state... so I'm trying to figure out why is this second redirect happening.

Extract from console.log when clicking back button: enter image description here

My first candidate is:

$urlRouterProvider.otherwise('/tab/alerts');

You shouldn't use ui-route with standard routing, so you should comment this line an add the next in your controller:

  console.log('going to default state');
  $state.go('tab.alerts');

EDIT

The second transition is made by the ionic framework, I have put a breakpoint and check the callstack, check this captures:

1- first call when back is clicked: enter image description here

2- The ionic framework detects a change in the url and.. fires the second transition: enter image description here

I'm going to read more this framework to see if I understand why is this happening... I keep you updated.

Carlos Verdes
  • 3,037
  • 22
  • 20
0

You should pass the name of the state to $state.go(), not the state object.

So $state.go($scope.from.name) should solve this for you.

Martin
  • 15,820
  • 4
  • 47
  • 56
  • yes it should but it doesn't that's my problem, I forgot to add the .name back after I tested – Sjoerd de Wit Nov 04 '14 at 11:33
  • 1
    I had a look at your pen, and for some reason it is successfully redirecting to tab.volgend but then again redirecting to tab.alerts from there. – Abhishek Jain Nov 04 '14 at 11:42
0

why not simply use

$state.go('stateName', {//for stateParams}, { reload: true });
Charlie
  • 177
  • 6
0

This happened with me too. Try transitionTo method, instead of go

$state.transitionTo($scope.from.name);

This worked out in my case.

More details can be found here

Edit : I forked your original codepen and it worked there too.. Forked Codepen here

Manish Kr. Shukla
  • 4,447
  • 1
  • 20
  • 35
  • when I open your pen it doesn't work, tried to apply it to my project and didn't work their either, still get redirected to the alert template still thanks for the input – Sjoerd de Wit Nov 04 '14 at 12:17
  • For those wondering, we have a question on the [difference between $state.transitionTo() and $state.go() in AngularJS](http://stackoverflow.com/q/21105528/531762). It should be noted that `$state.go()` is recommended in most situations. – Thunderforge Sep 19 '16 at 19:31