所以我尝试在我的应用程序中使用 Angular 路由。问题是路由“/”不会自动初始化。所以我首先转到“/”,页面的主要内容是空的(模板根本没有加载),我单击指向/settings的链接并加载设置,然后单击指向“/”的链接,这模板正确初始化的时间。那么我该如何让这个东西在一开始就初始化呢?
这是我的路由配置:
app.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: '/profile.html',
controller: 'ProfileController',
}).when('/profile', {
templateUrl: '/profile.html',
controller: 'ProfileController',
}).when('/settings', {
templateUrl: '/settings.html',
controller: 'SettingsController'
});
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
});
我尝试从 Controller 调用 $location.url("/profile") ,它确实有帮助,但 url 必须更改,我宁愿保留“/”
最佳答案
使用otherwise({redirectTo: "/"})
更新:这里应该是这样的
$routeProvider
.when('/', {
templateUrl: '/profile.html',
controller: 'ProfileController',
}).when('/profile', {
templateUrl: '/profile.html',
controller: 'ProfileController',
}).when('/settings', {
templateUrl: '/settings.html',
controller: 'SettingsController'
}).otherwise({ redirectTo: '/' });
关于javascript - 如何初始化 Angular 默认路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32540162/