你可以用不同的方式来做。取决于你想要什么以及你使用什么样的路由器:
如果您使用的是ng-route
$routeProvider
.when('/main' , {templateUrl: 'main.html'})
.when('/somePage', {templateUrl: 'somePage.html'})
.otherwise({redirectTo: '/main'});
app.controller('myCtrl', function ($location) {
$scope.click = function () {
$location.path('/somePage');
}
});
如果您使用的是ui-router
$stateProvider
.state('main', {
url: '/main',
templateUrl: 'main.html'
})
.state('somePage', {
url: '/somePage/:someParam',
templateUrl: 'somePage.html'
});
app.controller('myCtrl', function ($state) {
$scope.click = function () {
$state.go('somePage', { someParam: 'someValue'});
}
});
如果您使用的是ui-router >= 版本1.0.0
这将向您展示如何处理基于状态的重定向。在某些情况下,当您必须根据某些条件处理该路由上的重定向时,这可能会有所帮助。
$stateProvider
.state('main', {
url: '/main',
templateUrl: 'main.html',
redirectTo: (trans) => {
if (trans.params().redirectToSomePage) {
return { state: 'somePage', params: { someParam: 'someValue' } };
}
}
})
.state('somePage', {
url: '/somePage/:someParam',
templateUrl: 'somePage.html'
});