2017-01-20 3 views
1

ユーザが状態cloudに入ると、現在の変数が見つかるかどうかを調べ、存在しない場合は別の状態にリダイレクトします。しかし何らかの理由で、私はリダイレクトで次のエラーが発生します。状態をリダイレクトします:nullはオブジェクトではありません(名前を評価する)

/newに直接行けばエラーにはなりません。リダイレクトが発生したときのみ。

誰でも問題の原因を知ることができますか?

.config(function($stateProvider, $urlRouterProvider, $locationProvider) { 

    $locationProvider.html5Mode(true); 
    $urlRouterProvider.otherwise('/cloud'); 

    var billable = ['$rootScope', '$state', function ($rootScope, $state) { 
     if(!$rootScope.billable) $state.go('new'); 
    }]; 

    $stateProvider 

    .state('new', { 
     url: '/new', 
     views: { 'main': { templateUrl: 'pages/templates/new.html', controller: 'new' } }, 
    }) 

    .state('cloud', { 
     url: '/cloud', 
     views: { 'main': { templateUrl: 'pages/templates/cloud.html', controller: 'cloud' } }, 
     onEnter: billable 
    }) 

}) 

Error: null is not an object (evaluating 'name') [email protected]http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:3953:87 http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:3924:21 [email protected]http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js:14720:33 [email protected]http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:3678:32 http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:3554:30 [email protected]http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:474:48 [email protected]http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:470:33 http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:449:20 http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:3557:46 [email protected]http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js:331:24 [email protected]http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:3551:16 [email protected]http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js:13189:29 http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js:13205:39 [email protected]http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js:14217:36 http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js:14440:33 [email protected]http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js:4905:15 http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js:5285:33

+0

代わりconfig'を 'でそれを定義する')あなたは(.RUN 'に' billable'を移動しようとしたがありますか? – aaronmallen

答えて

0

あなたはこの設定されている方法で、私は、アプリケーションがまだブートストラップされていないため、任意のstatesがまだ定義されているとは思いません。さらに、このようなルートリダイレクトは通常configではなくrunメソッドに配置されます。

URLのパラメータに請求可能(ブール値または必要なもの)を渡して、それに基づいてリダイレクトすることができます。

すなわち

// I'm assuming you're using UI-Router 
.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider) { 
    $locationProvider.html5Mode(true); 
    $urlRouterProvider.otherwise('/cloud'); 
    $stateProvider 
    .state('new', { 
     url: '/new', 
     views: { 'main': { templateUrl: 'pages/templates/new.html', controller: 'new' } }, 
    }) 
    .state('cloud', { 
     url: '/cloud?billable', 
     views: { 'main': { templateUrl: 'pages/templates/cloud.html', controller: 'cloud' } }, 
    }) 
}]) 
.run(['$rootScope', '$state', function($rootScope, $state) { 
    $rootScope.$on("$stateChangeStart", function(e, toState, toParams, fromState, fromParams) { 
    if (!toParams.billable) { 
     e.preventDefault(); 
     $state.go('new'); 
    } 
    }); 
}]); 
+0

これはありがとう、私は私の請求可能なparamをつかむためにサービスを使用しようとするとdoesntは実行中に動作するようだ。なぜ私は少し混乱している – bryan

関連する問題