我正在尝试使用$state
进行简单的路由重定向。我无法让它发挥作用。这是我的代码:
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ng-token-auth'])
.run(function($ionicPlatform, $location, $rootScope, $state) {
$ionicPlatform.ready(function($state) {
$rootScope.$on('auth:login-success', function($state) {
console.log('success event triggered yo main');
// $location.path('main');
$state.go('main');
});
});
})
.config(function($stateProvider, $urlRouterProvider, $authProvider) {
$stateProvider
// setup an abstract state for the tabs directive
.state('main', {
url: '/main',
templateUrl: 'templates/main.html'
})
.state('home', {
url: '/home',
templateUrl: 'templates/home.html'
})
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/home');
});
这给了我以下错误:
ionic.bundle.js:25642 TypeError: $state.go is not a function
at app.js:28
at Scope.$broadcast (ionic.bundle.js:29477)
at ng-token-auth.js:191
at ionic.bundle.js:23384
at processQueue (ionic.bundle.js:27879)
at ionic.bundle.js:27895
at Scope.$eval (ionic.bundle.js:29158)
at Scope.$digest (ionic.bundle.js:28969)
at Scope.$apply (ionic.bundle.js:29263)
at done (ionic.bundle.js:23676)
我尝试从$ rootScope。$ on中删除$ state但是我收到了这个错误:
ionic.bundle.js:25642 TypeError: Cannot read property 'go' of undefined
at app.js:28
at Scope.$broadcast (ionic.bundle.js:29477)
at ng-token-auth.js:191
at ionic.bundle.js:23384
at processQueue (ionic.bundle.js:27879)
at ionic.bundle.js:27895
at Scope.$eval (ionic.bundle.js:29158)
at Scope.$digest (ionic.bundle.js:28969)
at Scope.$apply (ionic.bundle.js:29263)
at done (ionic.bundle.js:23676)
我该如何解决这个问题?
答案 0 :(得分:1)
尝试从$state
中删除$rootScope.$on
参数。我认为应该是:
$rootScope.$on('auth:login-success', function() {
console.log('success event triggered yo main');
// $location.path('main');
$state.go('main');
});
通过传递$state
作为参数,你基本上是这样做的:
$rootScope.$on('auth:login-success', function($state) {
var $state;
console.log('success event triggered yo main');
// $location.path('main');
$state.go('main'); //undefined...
});
您也可能需要$ionicPlatform.ready(function($state) {...
。您正在定义回调函数...而不是传入$state
。你认为发生的事情不是。
答案 1 :(得分:0)
您需要将依赖项设置为ui.router模块:
angular.module('starter', ['ionic', .... , 'ui.router'])