我将 AngularJs 与 Ruby on Rails 结合使用。
我将 html5 选项设置为 true in angular: $locationProvider.html5Mode(true)
但现在当我尝试在我的应用程序中浏览页面时,它只会更改浏览器 URL 栏中的 URL。但是除非我重新加载页面,否则它自己的页面将不会改变。只有这样它才会转到指定的页面。
这是我的 Angular 路由的样子:
@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")
]
要让这种路由发挥作用,还需要什么?
新的 Angular 路线:
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'
关于javascript - Angular html5模式不重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27196873/