加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 在onEnter中使用$state时,无法读取null的属性’@’

发布时间:2020-12-17 18:08:19 所属栏目:安全 来源:网络整理
导读:我从angular-ui-router获得以下错误: TypeError: Cannot read property '@' of null...TypeError: Cannot read property '@main' of null... 我已经对可能导致此错误的原因进行了一些研究,并得出结论,问题在于我在onEnter期间通过$state.go(state)在条件为f
我从angular-ui-router获得以下错误:

TypeError: Cannot read property '@' of null
...
TypeError: Cannot read property '@main' of null
...

我已经对可能导致此错误的原因进行了一些研究,并得出结论,问题在于我在onEnter期间通过$state.go(state)在条件为false时进行重定向另一个州

以下是关于github上同一问题的一些讨论:

> https://github.com/angular-ui/ui-router/issues/1234
> https://github.com/angular-ui/ui-router/issues/1434

我尝试了一些建议,但它们没有用.他们似乎没有提供解决方案,但只是指出问题,并根据this github讨论它应该解决,但我似乎无法让它工作.

我使用的是AngularJS v1.2.24和ui-router v0.2.11.

使用以下代码(简化):

.state('main',{
    abstract: true,url: '/main',templateUrl: '/main/_layout.html'
})
.state('main.home',{
    url: '/home',templateUrl: '/main/home/home.html',controller: 'HomeCtrl',onEnter: function ($state)
    {
        if (condition === true){
            $state.go('main.catch')
        }
    }
})
.state('main.catch',{
    url: '/catch',templateUrl: '/main/catch/catch.html',controller: 'CatchCtrl'
})

有没有办法让这个工作?或者我应该考虑一种完全不同的方法来实现相同的结果?

P.S.:可能的解决方法是在控制器中执行以下操作,

$scope.$on('$stateChangeSuccess',function(){
    // conditional redirect here
}

但我不想在HomeCtrl中这样做.

解决方法

我遇到过类似的情况,并用这种方式解决了:

.state('main.home',onEnter: function ($state)
    {
        if (condition === true){
            $timeout(function() {
                $state.go('main.catch')
            )};
        }
    }
})

诀窍在于,您希望让原始状态更改完成,而不是尝试在状态更改的中间重定向. $timeout允许原始状态更改完成,然后立即触发更改到所需状态.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读