8

我将介绍有问题的流程...

  • 我加载google.com(仅作为起点)
  • 去 app.com
  • 我导航到app.com/projects
  • 我导航到app.com/api/test(通过 window.location)
  • 我看到了原始 JSON(到目前为止很好......)
  • 我回击,网址更改为app.com/projects,但我仍然看到 JSON。
  • 我再次按下,网址更改为app.com,但我仍然看到 JSON。
  • 我再次按下,google.com加载。
  • 我继续前进,app.com加载正常……一切恢复正常

奇怪的是,我只html5Mode = true在 Webkit 中观察到这一点——<em>firefox 按预期工作......

. . .

首先,我的server.coffee看起来像这样:

app.get '/partials/:partial', routes.partials
app.get '/api/test', api.test
app.get '*', routes.index

基本上,所有请求都加载索引(它引导 Angular),视图/部分处理程序和一个以原始 JSON 响应的测试 api 路由除外。

. . .

(我使用ui-router模块来管理嵌套视图和 UI 状态;它使用$urlRouterProvider,这与 Angular 的非常相似$routeProvider

其次,我的app.coffee看起来像这样:

app = angular.module('app', ['ui-router'])
.config([
    '$stateProvider'
    '$locationProvider'
    '$urlRouterProvider'
    ($stateProvider, $locationProvider, $urlRouterProvider)->
        $urlRouterProvider
            .when('/api/test', [
                '$window'
                '$location'
                ($window, $location)->
                    $window.location.href = '/api/test'
            ])
            .otherwise('/')
        $stateProvider
            .state 'home',
                url: '/'
                templateUrl: 'partials/index'
                controller: 'IndexCtrl'
            .state 'projects',
                url: '/projects'
                templateUrl: 'partials/projects'
                controller: 'ProjectsCtrl'
        $locationProvider.html5Mode(true).hashPrefix '!'
])

由于一切都是异步的,我不得不使用$window访问window.location.href来触发页面刷新,以便服务器处理路由。

所以我的问题是,我可以在html5Mode不破坏后退按钮的情况下将触发页面刷新的链接与 Angular 混合使用吗?这只是一个 Webkit 错误,和/或有更好的方法吗?

理想情况下,我会让应用程序在 Angular 之外运行——但是像“关于”或“联系”页面(不需要动态或异步)之类的东西,将使用定期页面刷新直接从服务器提供......

帮助!

4

1 回答 1

18

Two options:

  1. If you're using <a /> tags, specify target="_self" to let AngularJS know it shouldn't capture their clicks (those links will be handled by browser normally). This is not a hack; this is normal documented behavior.
  2. Use $window.location = 'foo' as you have been doing. That is acceptable.
于 2013-05-03T09:22:45.220 回答