0

我正在尝试使用$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)

我该如何解决?

4

2 回答 2

1

尝试$state$rootScope.$on. 我认为应该是:

$rootScope.$on('auth:login-success', function() {
      console.log('success event triggered yo main');
      // $location.path('main');
      $state.go('main');
});

通过将 the$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. 你认为正在发生的事情并非如此。

于 2016-04-07T16:07:49.287 回答
0

您需要将依赖项设置为 ui.router 模块:

angular.module('starter', ['ionic', .... , 'ui.router'])
于 2016-04-07T16:09:33.100 回答