2017-03-17 91 views
0

这是路由配置:如何在一个控制器中更改AngularJS路由中的页面URL?

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属性?

回答

1

我这样做的方式很简单。在路由配置定义标题:

.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”。

相关问题