Angular UI Router,路由解析刷新按钮 [英] Angular UI Router, route resolve breaking refresh button

查看:19
本文介绍了Angular UI Router,路由解析刷新按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 Angular UI 路由器的问题.当我路由以下状态时,它完全正常.但是,当我刷新页面时,解析获取数据,将其注入控制器,但未加载视图.简而言之,状态在通过 ui-sref 链接或 $state.go 路由时可以正常工作,但在页面刷新时则不行.有没有人遇到过这个问题?

I am having an issue with Angular UI router. When I route the below state, it works completely fine. However, when I refresh the page, the resolve gets the data, it is injected into the controller, but the view does not load. In short, the state works fine when it is routed to via a ui-sref link or $state.go, but not when the page is refreshed. Has anyone encountered this issue before?

  $stateProvider.state('information', {
        templateUrl: 'information.html',  
        resolve:{
             'infoData': function($q, myFactory) {
                 var data = {};
                 data.first = myFactory.get(1);
                 return $q.all({first: data.first.$promise});  
              }
        },
        controller: function($scope, infoData){
          console.log(infoData);
        }
    }

推荐答案

我不知道我是否可以直接回答您的问题,但我也许可以为您指明正确的方向.你的症状和我的很像.

I don't know if I can directly answer your question, but I can maybe point you in the right direction. Your symptoms seemed similar to mine.

这个答案让我意识到 Angular UI Router 似乎默认吞下状态错误.在向 $stateChangeError 事件添加全局侦听器并记录错误后,我能够看到我的解析函数中的一些依赖项(在您的情况下,myFactory)当我刷新页面时没有加载.通过对应用程序其余部分的正常导航,这不是问题,因为它是我的几个正确加载它的控制器之间的共享依赖项.

This answer led me to realize that Angular UI Router seems to swallow state errors by default. Upon adding a global listener to the $stateChangeError event and logging the error, I was able to see that some of the dependencies in my resolve function (in your case, myFactory) weren't loaded when I was refreshing the page. This wasn't an issue via normal navigation of the rest of the application because it was a shared dependency amongst a couple of my controllers that had properly loaded it.

在我的项目中,我们将 RequireJS 与 Angular 一起使用,因此我的解决方案是将我的依赖项添加到我的新控制器的 define 列表中(似乎与您无关,所以我不会详细介绍).此外,就其价值而言,我的项目使用的是 Angular 1.2.13 和 Angular UI Router 0.2.10.

In my project, we're using RequireJS along with Angular, so the solution for me was to just add my dependency to the define list for my new controller (doesn't seem relevant for you, so I won't go into details). Also, for what it's worth, my project is using Angular 1.2.13 and Angular UI Router 0.2.10.

希望这有帮助...如果有的话,请为我引用的答案点赞!

Hope this helps... upvote the answer I referenced if it does!

这篇关于Angular UI Router,路由解析刷新按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆