角度 html5 模式不重定向

Angular html5 mode not redirecting

本文关键字:重定向 模式 html5 角度      更新时间:2023-09-26

我正在使用AngularJs和Ruby on Rails。

我将 html5 选项设置为 true 在角度:$locationProvider.html5Mode(true)

但是现在,当我尝试浏览应用程序中的页面时,它只会更改浏览器 URL 栏中的 URL。但是除非我重新加载页面,否则它本身的页面不会更改。只有这样,它才会转到指定的页面。

这就是我的角度路由的样子:

@test.config(['$routeProvider', '$locationProvider', ($routeProvider, $locationProvider) ->
    # Route for '/'
    $routeProvider.when('/', { templateUrl: '../../views/visitors/index.html.erb', controller: 'VisitorsCtrl' } )
    $routeProvider.when('/users', { templateUrl: '../../views/users/index.html.erb', controller: 'UsersCtrl' } )
    # Default
    $routeProvider.otherwise( { redirectTo: "/" } )
    $locationProvider.html5Mode(true)
])

在根页面上,我有一个元素:<h2><a ng-click="viewUsers()">Users</a></h2>

这里称viewUsers()

@test.controller 'VisitorsCtrl', ['$scope', '$location', '$http', ($scope, $location, $http) -> 
    $scope.viewUsers = ->
        $location.path('/users')
        console.log("Redirected")
]

要使这种路由工作,还需要什么?

新的角度路线:

Test.config ($routeProvider, $locationProvider) ->
    $routeProvider
        .when '/',
            templateUrl: '../../views/visitors/index.html.erb',
            controller: 'VisitorsCtrl'
        .when '/users',
            templateUrl: '../../views/users/index.html.erb',
            controller: 'UsersCtrl'

请尝试使用以下方法:

$window.location.href = '/users'