Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 重定向状态:null不是对象(正在计算名称)_Javascript_Angularjs_Angular Ui Router - Fatal编程技术网

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');
    }
  });
}]);