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

AngularJs UI-Router – 页面刷新$state.current现在为空

发布时间:2020-12-17 07:34:45 所属栏目:安全 来源:网络整理
导读:我有一个使用ui-router的Angular应用程序,每当我刷新页面时我都会遇到问题.我正在使用嵌套视图,命名视图来构建应用程序.每当我刷新页面时,ui-router都不会重新加载当前状态,只是将页面留空. 页面加载$state.current等于 Object {name: "",url: "^",views: nu
我有一个使用ui-router的Angular应用程序,每当我刷新页面时我都会遇到问题.我正在使用嵌套视图,命名视图来构建应用程序.每当我刷新页面时,ui-router都不会重新加载当前状态,只是将页面留空.

页面加载$state.current等于

Object {name: "",url: "^",views: null,abstract: true}

我正在通过$http从.json文件中读取导航并循环遍历各个州.所以这就是我能展示的:

stateProvider.state(navElement.stateName,{
    url: navElement.regexUrl ? navElement.regexUrl : url,searchPage: navElement.searchPage,//something custom i added
    parent: navElement.parent ? navElement.parent : "",redirectTo: navElement.redirectTo,views: {
        'subNav@index': {
            templateUrl: defaults.secondaryNavigation,controller: 'secondaryNavigationController as ctrl' //static
        },'pageContent@index': {
            template: navElement.templateUrl == null 
                                              ? '<div class="emptyContent"></div>' 
                                              : undefined,templateUrl: navElement.templateUrl == null 
                                              ? undefined 
                                              : navElement.templateUrl,controller: navElement.controller == null 
                                              ? undefined 
                                              : navElement.controller + ' as ctrl'
        }
    }
});

为嵌套的json对象中的每个项执行此代码.如果还有其他任何有用的东西,请告诉我.

有一个问题: AngularJS – UI-router – How to configure dynamic views有一个答案,显示了如何做到这一点.

What is happening? On refresh,the url is evaluated sooner,then states are registered. We have to postpone that. And solution is driven by UI-Router native feature deferIntercept(defer)

$urlRouterProvider.deferIntercept(defer)

如文件中所述:

Disables (or enables) deferring location change interception.

If you wish to customize the behavior of syncing the URL (for example,if you wish to defer a transition but maintain the current URL),call this method at configuration time. Then,at run time,call $urlRouter.listen() after you have configured your own $locationChangeSuccess event handler.

简而言之,我们将在配置阶段停止URL处理:

app.config(function ($urlRouterProvider) {

  // Prevent $urlRouter from automatically intercepting URL changes;
  // this allows you to configure custom behavior in between
  // location changes and route synchronization:
  $urlRouterProvider.deferIntercept();

})

一旦我们从JSON配置了所有动态状态,我们将在.run()阶段重新启用它:

.run(function ($rootScope,$urlRouter,UserService) {

  ...

    // Once the user has logged in,sync the current URL
    // to the router:
     $urlRouter.sync();

    // Configures $urlRouter's listener *after* your custom listener
    $urlRouter.listen();
});

链接Q & A有一个plunker

(编辑:李大同)

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

    推荐文章
      热点阅读