Angular UI Router, route resolve breaking refresh button

天涯浪子 提交于 2019-12-11 13:07:16

问题


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

回答1:


I've encountered this a few times. In my routes.js file I always use html5 mode, gets rid of the hash bang that angular defaults to.

// Set HTML5 mode to true
$locationProvider.html5Mode(true).hashPrefix('!');

I also have this in my .htaccess

RewriteEngine on
#let angular do its thing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.html [NC,L]



回答2:


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.

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.

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!



来源:https://stackoverflow.com/questions/23400055/angular-ui-router-route-resolve-breaking-refresh-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!