AngularJS路由有问题。我有以下代码:http://pastebin.com/Q2FqRVrc我不知道如何添加短配置文件,例如:http://domain.tld/username
有什么诀窍吗?
看起来您找到的链接是用于使用内置的ng路由指令的,但您已经用angular ui router标记了这个问题。这就是使用ui路由器的$stateProvider
来配置应用程序的配置。
angular.module('myApp', ['ui.router'])
.config(function ($stateProvider) {
// Configure my routes with ui-router
$stateProvider
.state('user-profile', {
url: '/:username',
templateUrl: 'app/profiles/template.html',
controller: 'UserProfileCtrl',
controllerAs: 'upCtrl'
})
// These are nested states, under `user-profile`
.state('user-profile.images', {
template: '<div my-images-directive></div>'
})
.state('user-profile.friends', {
template: '<div my-friends-directive></div>'
})
.state('otherPage', {
url: '/other-page',
templateUrl: 'app/otherpage/template.html'
});
});
要转到一个状态,您需要使用$state对象的$state.go
方法。
$state.go('user-profile', {username: $scope.user.name});
要去除散列#
,您需要使用$locationProvider.html5Mode(true)
打开HTML5模式