如何在一个控制器中更改 AngularJS 路由中页面的 URL?
How to change URL of the page in AngularJS routing in one controller?
这是路由配置:
tapp.config(function ($routeProvider) {
$routeProvider.when('/', {
title: 'Home',
controller: 'tCtrlr',
templateUrl: '../HTML/Index.html',
}),
$routeProvider.when('/Tasks', {
title: 'Tasks',
controller: 'tCtrlr',
templateUrl: '../HTML/Tasks.html',
}),
$routeProvider.when('/MyTasks', {
title: 'My Tasks',
controller: 'tCtrlr',
templateUrl: '../HTML/MyTasks.html',
})
在未设置 title
的页面之间导航时,我已经阅读了一些关于此问题的 SO 线程,但大多数人假设我对每个页面都有一个控制器。那么如何在when
函数中得到title
属性呢?
我的方法很简单。在路由配置中定义标题:
.when('/dashboard', {
title : 'My Dashboard',
templateUrl : 'templates/sections/app-dashboard.html',
controller : 'DashboardController'
})
然后你监听 $routeChangeSuccess 事件并设置 document.title。在应用程序 运行 块中(最好的地方)。
app.run(['$rootScope', '$route', function($rootScope, $route) {
$rootScope.$on('$routeChangeSuccess', function() {
document.title = $route.current.title;
});
}]);
这种方法的好处是它可以让您避免更多的绑定 ng-bind="title".
这是路由配置:
tapp.config(function ($routeProvider) {
$routeProvider.when('/', {
title: 'Home',
controller: 'tCtrlr',
templateUrl: '../HTML/Index.html',
}),
$routeProvider.when('/Tasks', {
title: 'Tasks',
controller: 'tCtrlr',
templateUrl: '../HTML/Tasks.html',
}),
$routeProvider.when('/MyTasks', {
title: 'My Tasks',
controller: 'tCtrlr',
templateUrl: '../HTML/MyTasks.html',
})
在未设置 title
的页面之间导航时,我已经阅读了一些关于此问题的 SO 线程,但大多数人假设我对每个页面都有一个控制器。那么如何在when
函数中得到title
属性呢?
我的方法很简单。在路由配置中定义标题:
.when('/dashboard', {
title : 'My Dashboard',
templateUrl : 'templates/sections/app-dashboard.html',
controller : 'DashboardController'
})
然后你监听 $routeChangeSuccess 事件并设置 document.title。在应用程序 运行 块中(最好的地方)。
app.run(['$rootScope', '$route', function($rootScope, $route) {
$rootScope.$on('$routeChangeSuccess', function() {
document.title = $route.current.title;
});
}]);
这种方法的好处是它可以让您避免更多的绑定 ng-bind="title".