2017-05-27 23 views
2

我動態改變我的冠軍AngularJs,使用下面的代碼:如何在Angular Js中使用變量來更改頁面標題?

app.run(['$rootScope', function($rootScope) { 
    $rootScope.$on('$routeChangeSuccess', function (event, current, previous) { 
     $rootScope.title = current.$$route.title; 
    }); 
}]); 

app.config(['$routeProvider', function($routeProvider) { 
    $routeProvider 
     .when('/', { 
      title: 'My Tagline', 
      controller : 'IndexController', 
      templateUrl : 'static/app_partials/index.html' 
     }) 
     .when('/categories/:categoryPk/', { 
      title: $scope.category.name, 
      controller : 'CategoryController', 
      templateUrl : 'static/app_partials/category.html' 
     }) 
     .otherwise({ redirectTo : '/' }); 
}]); 

的Html

<title ng-bind="'My Brand - ' + title">My Brand</title> 

所以,對於IndexController,標題只是一個簡單的字符串,因此這種方法很好地工作。但是,對於我的CategoryController,我希望標題是從一個變量中生成的,具體來說,是$scope.category.name,我在我的控制器代碼中進行了初始化。我怎樣才能做到這一點?任何幫助?

+0

你也可以在這裏找到答案:https://stackoverflow.com/questions/12506329/how-to-dynamic-change-header-based-angularjs-partial-view –

回答

3

使用ng-bind-template如下:

<title ng-bind-template="My Brand - {{$root.title}}">My Brand</title> 
+0

也必須從路由配置中刪除'title:$ scope.category.name,'......沒有' $ scope' there – charlietfl

+0

然後,如何將我的變量作爲控制器的標題? –

+0

@TahmidKhanNafee你可以在'routeChangeSuccess'甚至處理程序中執行它 – mastilver

0

ng-meta看看在你的AngularJS設置動態meta標籤單頁應用

相關問題