AngularJS:路由提供程序不';在根上时不工作
我有一个AngularJS应用程序,它使用AngularJS:路由提供程序不';在根上时不工作,angularjs,routes,single-page-application,Angularjs,Routes,Single Page Application,我有一个AngularJS应用程序,它使用$routeProvider和$stateProvider。除了/之外,我可以让我的所有路线/状态正常工作 这是我的app.js文件 var MailStash = angular.module("MailStash", ['ui.compat', 'ngResource', 'ngSanitize', 'ui.directives']). config(function($stateProvider, $routeProvider, $
$routeProvider
和$stateProvider
。除了/
之外,我可以让我的所有路线/状态正常工作
这是我的app.js文件
var MailStash = angular.module("MailStash", ['ui.compat', 'ngResource', 'ngSanitize', 'ui.directives']).
config(function($stateProvider, $routeProvider, $urlRouterProvider) {
$routeProvider
.when('/', { controller: ListCtrl, templateUrl: '/js/partials/list.html' });
$stateProvider
.state('templates', {
url: '/templates',
abstract: true,
templateUrl: '/js/partials/list.html',
controller: ListCtrl,
})
.state('templates.list', {
// parent: 'templates',
url: '',
views: {
'main_content': {
templateUrl: '/js/partials/templates.new.html',
controller:CreateCtrl,
},
}
})
.state('templates.view', {
parent: 'templates',
url: '/{templateId}',
views: {
'main_content': {
templateUrl: '/js/partials/templates.view.html',
controller: ViewCtrl,
},
},
})
.state('templates.new', {
url: '/new',
views: {
'main_content': {
templateUrl: '/js/partials/templates.new.html',
controller: CreateCtrl,
},
},
})
.state('templates.edit', {
parent: 'templates',
url: '/edit/{templateId}',
views: {
'main_content': {
templateUrl: '/js/partials/templates.edit.html',
controller: EditCtrl,
},
},
})
}
)
.run(
[ '$rootScope', '$state', '$stateParams',
function ($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
}]
);
...
当我转到/
时,什么也不会发生,但当我转到/#模板
时,相应的视图和控制器会启动
有人能看到我的
$routeProvider有什么问题吗?为什么转到/
没有任何作用?您可以这样指定默认路由
$routeProvider
.when('/templates', { controller: ListCtrl, templateUrl: '/js/partials/list.html' })
.otherwise({ redirectTo: '/templates' });
希望这会有帮助 为什么不直接使用$urlRouterProvider
并发送到/
$urlRouterProvider.otherwise('/');
然后为/
$stateProvider.state({
name: 'home',
url: '/',
controller: 'HomeCtrl',
templateURL: 'home.html'
});
否则,如果url不正确,则重定向到错误页面更有用。但是您可以尝试添加
以引用索引中的基本位置,并添加$locationProvider.html5Mode(true);在将配置函数中的路由声明为enabled之后,否则需要将#添加到url
我希望这对你有帮助