当用户进入状态cloud
时,它会检查是否找到当前变量,如果未找到,则将其重定向到其他状态。但由于某种原因,我在重定向上收到以下错误。
如果我直接转到 / 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
})
})
错误:null不是对象(评估'name') 更新视图@ 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 $ @广播http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js:14720:33 负荷@ 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 继续@ http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:474:48 调用@ 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 的forEach @ http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js:331:24 resolveViews @ http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:3551:16 processQueue @ 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 $消化@ 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 completeOutstandingRequest @ 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 :(得分:0)
您配置此方式的方式,我不相信任何states
已被定义,因为该应用程序尚未引导。此外,此类路由重定向通常位于run
方法中,而不是config
中。
你可以在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');
}
});
}]);