Angular using $routeParams in Page title

a 夏天 提交于 2019-12-05 08:29:10
kah608

Can't you just access it like this:

$routeParams[$routeParams.title]

I think that is what you are asking, but I'm not quite sure...

In the routeChangeSuccess handler I added

var title = currentRoute.$$route.title;
if (title && $routeParams){
    for(var paramName in $routeParams) {
        title = title.replace(new RegExp(':' + paramName, 'g'), $routeParams[paramName]);
    }
}
$rootScope.title = title;

Modify your runner to check for the type of title like this,

    .run(['$rootScope', '$route', '$routeParams', function($rootScope, $route, $routeParams) {
    $rootScope.$on('$routeChangeSuccess', function() {
       if(!!$route.current.title) {
           if(typeof $route.current.title == 'function') {
               document.title = $route.current.title($routeParams);
           } else if (typeof $route.current.title == 'string') {
               document.title = $route.current.title;
           }

       }
    });
}]);

And define the function in routeProvider as you want to compose the title, like,

$routeProvider.when('/demo/:name/:idNumber', {
     title : function(params){ return "Name: " + params.name;},
     templateUrl: 'partials/details.html', 
     controller: 'AppDetails'}); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!