2015-11-01 137 views
1

我如何使用帖子的参数标题来显示浏览器的标题?Angular UI路由器,标题动态

我在我的路线上使用pageTitle参数,但如果直接放置:slug作为值,则不起作用。

.state('posts',{ 
    url   : '/blog/:slug', 
    templateUrl : 'content/templates/single.html', 
    controller : 'SinglePostController', 
    data: { 
     pageTitle: 'title' 
    }, 
    access: { 
     requiredLogin: false 
    } 
}) 
+0

你想把'slug'放在你的浏览器标题上吗?但那么其他州的标题是什么呢? –

回答

1

data : {}设置是静态的。

看到类似:

如果你想要一些动态特征利用resolve : {}

一些链接&关于解决例子和Q

EXTEND:一个简单的(真的很幼稚,但工作)例如如何使用resolve$rootScope管理浏览器的标题(check it here):

$stateProvider 
    .state('home', { 
     url: "/home", 
     templateUrl: 'tpl.html', 
     resolve: { 
     'title': ['$rootScope', function($rootScope){ 
      $rootScope.title = "Other title"; 
     }], 
     } 
    }) 
    .state('parent', { 
     url: "/parent", 
     templateUrl: 'tpl.html', 
     resolve: { 
     'title': ['$rootScope', function($rootScope){ 
      $rootScope.title = "Title from Parent"; 
     }], 
     } 
    }) 
    .state('parent.child', { 
     url: "/child", 
     templateUrl: 'tpl.html', 
     controller: 'ChildCtrl', 
     resolve: { 
     'titleFromChild': ['$rootScope', function($rootScope){ 
      $rootScope.title = "Title from Child"; 
     }], 
     } 
    }) 

而且这可能是HTML

<!DOCTYPE html> 
<html ng-app="MyApp" ng-strict-di> 

    <head> 
    <title>{{title}}</title> 

试一试here

这里的挑战是 - 什么对导航执行从子到父,但它可以通过移动设置到控制器,并与$scope.$on('detsroy'工作要做......

这里是adjusted plunker

.state('parent.child', { 
     url: "/child", 
     templateUrl: 'tpl.html', 
     controller: 'ChildCtrl', 
     // no resolve, just controller fills the target 
    }) 

.controller('ChildCtrl', ['$rootScope', '$scope', function ($rootScope, $scope) { 
    var title = $rootScope.title; 
    $rootScope.title = "Title from Child"; 
    $scope.$on('$destroy', function(){$rootScope.title = title}); 
}]) 
+0

好吧,现在一步作为slug参数来解决?直接由stateParams?并用rootScope设置? –

+0

@MarcoRiesco,如果我明白你的问题,这应该是最适合你的:http://stackoverflow.com/q/30268800/1679310 –

+1

我的问题是浏览器标题 –