Javascript 重定向状态:null不是对象(正在计算名称)
当用户进入状态Javascript 重定向状态:null不是对象(正在计算名称),javascript,angularjs,angular-ui-router,Javascript,Angularjs,Angular Ui Router,当用户进入状态cloud时,它会检查是否找到当前变量,如果找不到,它会将它们重定向到不同的状态。但是由于某种原因,我在重定向中遇到了以下错误 如果直接转到/new,则不会显示错误。仅当重定向发生时 有人知道问题是什么吗 .config(function($stateProvider, $urlRouterProvider, $locationProvider) { $locationProvider.html5Mode(true); $urlRouterProvider.oth
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不是对象(正在计算“名称”)
更新视图@
$broadcast@
装载@
进行@
援引@
弗雷奇@
解决视图@
处理队列@
$digest@
完成未完成的请求@
按照您的配置方式,我认为尚未定义任何
状态
,因为应用程序尚未启动。此外,像这样的路由重定向通常放置在run
方法中,而不是config
中
您可以在url的参数中传递billable(作为布尔值或任何您需要的值),并基于此重定向
i、 e
按照您的配置方式,我认为尚未定义任何
状态
,因为应用程序尚未启动。此外,像这样的路由重定向通常放置在run
方法中,而不是config
中
您可以在url的参数中传递billable(作为布尔值或任何您需要的值),并基于此重定向
i、 e
您是否尝试过将
billable
移动到.run()
而不是在config
中定义它?您是否尝试过将billable
移动到.run()
而不是在config
中定义它?为此,我尝试使用一个服务来获取我的billable参数,但在运行过程中它似乎不起作用。我有点困惑为什么会这样,我尝试使用一个服务来抓取我的付费参数,但它似乎在运行中不起作用。我有点困惑为什么
// 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');
}
});
}]);