$ state.go(' state.name')未正确更改状态

时间:2014-04-15 07:43:37

标签: angularjs angular-ui-router

我试图以编程方式在状态之间移动(使用ui.router)而无需用户点击任何内容。 http://angular-ui.github.io/ui-router/site/#/api/ui.router.state。$州的文档似乎表明我可以使用$ state.go(&#39; state.name&#39;)执行此操作,但我无法完成此工作。< / p>

我已经创建了一个用来证明我的问题的傻瓜。理想情况下,国家应切换到显示&#34;成功&#34;而不是&#34;失败&#34;什么时候跑。如果您查看控制台,它会说它无法读取该属性&#39; go&#39;未定义的。我怎样才能以编程方式改变状态?

谢谢!

http://plnkr.co/edit/MSLbKaKzmuXPud7ZcKqs

编辑:更清楚地指明我使用ui.router

1 个答案:

答案 0 :(得分:12)

2件事:

  1. 您忘了将$state注入app.run()
  2. 使用$stateChangeSuccess代替

    .run(['$rootScope','$state', function($rootScope, $state) { $rootScope.$on('$stateChangeSuccess', function(event, next) { $state.go('root.other'); })}]);

  3. Working plunk