2017-05-27 19 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-on-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

あなたAngularJSで動的なメタタグを設定するng-metaを見てみましょう単一ページのアプリケーション

関連する問題