2015-11-01 132 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

我的問題是瀏覽器標題 –