简体   繁体   English

$ state.go不会重定向到给定路径

[英]$state.go doesn’t redirect to given path

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 我制作了一个Codepen,向您展示我的意思,因为我真的不知道如何很好地解释它,请参阅我在控制台中创建的日志

  .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 Codepen

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. 另一个问题是,当您单击后退按钮时,状态将更改为tab.volgend(是正确的),但是此后将变为tab.alert状态...所以我试图弄清楚为什么这是第二个重定向发生。

Extract from console.log when clicking back button: 单击后退按钮时,从console.log中提取: 在此处输入图片说明

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: 您不应该将ui-route与标准路由一起使用,因此应该在命令行中添加以下注释:

  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: 第二个转换是由ionic框架完成的,我放置了一个断点并检查了调用堆栈,检查了以下捕获:

1- first call when back is clicked: 1-单击返回时的第一个呼叫: 在此处输入图片说明

2- The ionic framework detects a change in the url and.. fires the second transition: 2-离子框架检测到URL中的更改并触发第二个转换: 在此处输入图片说明

I'm going to read more this framework to see if I understand why is this happening... I keep you updated. 我将阅读更多有关此框架的信息,以了解我是否理解为什么会发生...我会及时通知您。

You should pass the name of the state to $state.go(), not the state object. 您应该将状态名称传递给$ state.go(),而不是状态对象。

So $state.go($scope.from.name) should solve this for you. 所以$ state.go($ scope.from.name)应该为您解决这个问题。

为什么不简单地使用

$state.go('stateName', {//for stateParams}, { reload: true });

This happened with me too. 我也是这样 Try transitionTo method, instead of go 尝试transitionTo方法,而不是去

$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 编辑:我叉你原来codepen并在那里工作太.. 叉形Codepen这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM